0

我正在尝试根据它的行号生成 td 元素的 id。

我相信 td id 必须是唯一的。也许我错了?因此,我试图将行号与一些文本连接起来,但似乎无法让它工作。

我们在我们的网站上使用 jQuery,我可以成功创建基于变量的其他 id,但我有点卡在这个上。这是一个有效的例子

<td id="person-id${person.id}">${person.name}</td>

html 稍后将类似于:

<td id="person1"></td><td id="person2"></td><td id="person3"></td> etc

我希望为另一张桌子做这样的事情,但遇到了一些麻烦:

<td id="row-number("+rowIndex+")">some text here</td>

任何帮助,将不胜感激。

4

1 回答 1

1

我假设您想根据列号而不是行号分配 id。如果我在这个假设中是正确的,你可以像这样使用 jQuery 来做到这一点:

$("tr").each(function() {
 $(this).children().each(function() {
 var n = $(this).index();
 $(this).attr('id','person'+n);
 });
});

否则,如果您确实希望将行号分配给 id,它将是:

$("tr").each(function() {
 $(this).children().each(function() {
 var n = $(this).parent().index();
 $(this).attr('id','person'+n);
 });
});

瞧。

于 2013-01-15T15:19:42.977 回答