2016-07-25 47 views
0

我想要顯示我的表格列表的索引,而不需要點擊這些在SO和其他來源中的大多數示例中。如何在動態表格列表中指示<tr>表格行的位置

<script src="../js/jquery-2.0.2.min.js"></script> 
<table id="tableID" width="200" border="1"> 
    <tr> 
    <td class="sm">5</td> 
    <td class="pos"></td> 
    </tr>  
    <tr> 
    <td class="sm">4</td> 
    <td class="pos"></td> 
    </tr> 
    <tr> 
    <td class="sm">5</td> 
    <td class="pos"></td> 
    </tr> 
</table> 
<script> 
var position = getElementsByClassName("pos").index();; 
document.getElementsByClassName("pos")[0].textContent = position; 
</script> 

我想上表給喜歡

enter image description here

回答

1
$('td.pos').each(function(){ 
$(this).html($(this).parent().index()+1); 
}); 

這裏的結果是DEMO