2014-01-22 37 views
1

我想返回給定tr的索引,當我單擊td內的元素時。單擊返回數據表tr指數

的HTML

<table class="tablesorter-js"> 
    <thead> 
     <tr> 
      <th class="txt-align-left>Builder</th> 
      <th class=" txt-align-left " jobs">Current jobs</th> 
      <tbody> 
       <tr> 
        <td>some text <a class="link" href="#">A link</a> 

        </td> 
        <td>tome more text</td> 
       </tr> 
       <tr> 
        <td>some text <a class="link" href="#">A link</a> 

        </td> 
        <td>tome more text</td> 
       </tr> 
      </tbody> 
</table> 

的JavaScript

$('.link').click(function(e { 
e.preventDefault(); 
console.log(oTable.fnGetPosition(this)) 
}); 

我知道我可以調用當前TR的索引與此

var oTable = $('.tablesorter-js').dataTable(); 
$('.tablesorter-js tbody tr').click(function() { 
    var pos = oTable.fnGetPosition(this)  
    // I want the position of the tr element when I click on .link    
    return pos; 
}); 

我如何獲得該指數當我單擊.link時,當前tr元素?

回答

1
$('.link').click(function(e) { 
    e.preventDefault(); 
    console.log($(this).closest('tr').index()); 
}); 
+0

工作就像一個魅力 – Newcoma

0

你可以得到當前<tr>元件,其鏈接被點擊下面的jQuery代碼的指數:

$('.link').click(function(){ 
    alert($(this).closest('tr').index()); 
}); 

Fiddle Demo

0

使用指數()jQuery的功能

var oTable = $('.tablesorter-js').dataTable(); 
$('.tablesorter-js tbody tr').click(function() { 
    //var pos = oTable.fnGetPosition(this)  
    var idx = $(this).index(".tablesorter-js tbody tr");  
    return idx; 
}); 

$('.link').click(function(e) { 
    e.preventDefault(); 
    console.log($(this).closest('tr').index(".tablesorter-js tbody tr")); 
}); 

有關詳細信息.index() JQuery