【发布时间】:2021-10-03 19:15:23
【问题描述】:
我创建了一个具有以下样式的表格。现在我想使用jQuery 将第一个 td 值设为 1,2,3。
<html>
<head>
<style type='text/css'>
table {
counter-reset: rowNumber;
}
table tr {
counter-increment: rowNumber;
}
table tr td:first-child::before {
content: counter(rowNumber);
min-width: 1em;
margin-right: 0.5em;
}
</style>
</head>
<body>
<table border="1" id="MyTable">
<tr>
<td></td> <td>blue</td>
</tr>
<tr>
<td></td><td>red</td>
</tr>
<tr>
<td></td><td>black</td>
</tr>
</table>
</body>
</html>
我尝试使用以下脚本,但它将行计数器显示为文本。如何获取第一个 td 的值?
<script>
$("#MyTable").find('tr').each(function (i, el) {
$(this).find('td:eq(0)').each(function () {
console.log(window.getComputedStyle(this, ':before').content);
});
});
</script>
【问题讨论】:
-
你的意思是要获取每个表格行的第一个
td的值吗? -
是的。我想要行计数器值。
-
可能你想把 1,2,3 放在 DOM 上的 TD 中。你可以这样做: $("#MyTable").find('tr').each(function (i, el) { $(el).find("td").eq(0).text(i ); });
-
根据this post看来是不可能的。
标签: javascript jquery html-table