jQuery每个数字

时间:2012-04-04 10:32:21

标签: jquery each

我有想法制作像这样的表

 1. something1
 2. something2
 3. something3

我做了一张桌子:

<table>
   <tr><td id="rb">...
   <tr><td id="rb">...
</table>

所以在#rb中这个序列号从1开始。

我有这个jQuery代码,但它不起作用。你能帮忙:)。

            $('tr').each(function(index) 
            {
                  $('#rb').append(index);
             });

它只是制作

       012345

在第一个#rb

提前致谢!

2 个答案:

答案 0 :(得分:1)

您不应该为同一HTML文档中的多个元素提供相同的ID。您可以使用class="rb"代替。

$('tr').each(function(index) {
   $(this).find('.rb').append(index);
});

答案 1 :(得分:1)

您不能多次使用#id

相反,您可以使用.rb类,如:

$('tr').each(function(index){
   $(this).children('.rb')append(index+1);
});

或类似的方法只是选择每个tr的子项(可能不起作用,具体取决于您的HTML结构):

$('tr').each(function(index) { //index will start at 0
   $(this).children('td').append(index+1); //$(this) is the tr, its child element of td is selected, then the index is appended
});