为表中的行分配索引号



我想自动添加索引号到表中的每一行,就像我从bootstrap的网站截图中得到的一样,我尝试了多种解决方案,但一直得到所有行的-1索引,我真的很感激帮助,谢谢。

这对你有用吗?

// tbl is the table
var rows = tbl.rows; n = 1;
for (i=0; i<rows.length; i++) {
row = rows[i];
f = row.firstElementChild;
if (f) {
el = document.createElement(f.tagName);
el.innerHTML = (f.tagName == "TH") ? "#" : n;
row.insertBefore(el, f);
if (f.tagName != "TH") n++;
}
}

最新更新