我尝试使用 .length
通过 javascript 通过循环遍历整个表格并列出每行的数字,向每个表格行添加一个数字。我一直在努力工作,但我总是失败。请帮忙!这是我的表格:
<table class="mytable">
<thead>
<tr>
<th>#</th>
<th>First</th>
<th>Last</th>
</tr>
</thead>
<tbody>
<tr class="person">
<td class="number"></td>
<td>Bill</td>
<td>Sims</td>
</tr>
<tr class="person">
<td class="number"></td>
<td>Jenny</td>
<td>Jackson</td>
</tr>
<tr class="person">
<td class="number"></td>
<td>Milo</td>
<td>Resa</td>
</tr>
</tbody>
</table>`
所以理想情况下它看起来像 1 Bill Sims 等等。
最佳答案
试试这个
$(function(){
$('.mytable tr').each(function(i){
$(this).find('td:first').text(i); // or $(this).find('td.number').text(i);
//Use iterator's index argument
});
});
或
$(function(){
$('.mytable tr td.number').text(function(){
return $(this).closest('tr').index() + 1; // get the parent tr's index() add 1 to get the #
});
});
Fiddle
关于jquery - 将数字添加到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16908295/