我想知道如何为表行分配一个 id,系统地作为计数器。它可以是一个字符串+一个计数器,如下所示:
<table>
<tr id="Row1"> # it can be only a number => id="1"
<tr id="Row2"> # it can be only a number => id="2"
<tr id="Row3"> # it can be only a number => id="3"
.....
<tr id="Row5000"> # it can be only a number => id="5000"
</table
因为我有数千行,然后无法手动为它们分配 id。这就是为什么我想通过 XSLT 分配它们。有谁知道我该怎么做?谢谢。
最佳答案
//JavaScript
var table = document.querySelectorAll('table tr');
{
for(var i=0;i<table.length;i++){
table[i].setAttribute("id",i+1);
}
//jquery
$("table tr").each(function(index,object) {
object.attr("id",(index+1));
})
关于javascript - 表格行作为 HTML 中的计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49064085/