javascript - 表格行作为 HTML 中的计数器

标签 javascript html xml xslt

我想知道如何为表行分配一个 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/

相关文章:

xml - 在控制台中删除 .xml 文件的一部分

javascript - 如何从属性创建文件路径

java - JAXB 将引用列表绑定(bind)到另一个元素

javascript - jquery-file-upload 上传的字节超过文件大小

javascript - 仅在 Ctrl + 滚动时缩放

javascript - 在我的网站中包含网站的一部分

html - CSS3 动画幻灯片/隐藏导航影响其他元素

javascript - 如何隐藏表格标题列

javascript - 带有 *ngIf 的 Angular 2 条件循环

javascript - 弹球破砖游戏,砖 block 弹不起来