HTMLTableRowElement: sectionRowIndex 属性
HTMLTableRowElement 接口的只读属性 sectionRowIndex 表示行在当前 section(<thead>、<tbody> 或 <tfoot>)中的位置。
值
行的索引,如果行不属于该 section,则为 -1。
示例
此示例使用 JavaScript 来标记 tbody 中所有行的编号。
HTML
html
<table>
<thead>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Bananas</td>
<td>$2</td>
</tr>
<tr>
<td>Oranges</td>
<td>$8</td>
</tr>
<tr>
<td>Top Sirloin</td>
<td>$20</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td>$30</td>
</tr>
</tfoot>
</table>
JavaScript
js
const rows = document.querySelectorAll("tbody tr");
rows.forEach((row) => {
const z = document.createElement("td");
z.textContent = `(row #${row.sectionRowIndex})`;
row.appendChild(z);
});
结果
规范
| 规范 |
|---|
| HTML # dom-tr-sectionrowindex |
浏览器兼容性
加载中…