HTMLTableRowElement: rowIndex 屬性
HTMLTableRowElement 介面的只讀屬性 rowIndex 表示行在整個 <table> 中的位置。
即使 <thead>、<tbody> 和 <tfoot> 元素在 HTML 中順序混亂,瀏覽器也會按正確的順序渲染表格。因此,行數是從 <thead> 到 <tbody>,再從 <tbody> 到 <tfoot> 進行計數。
值
行的索引,如果該行不屬於表格,則為 -1。
示例
本示例使用 JavaScript 為表格中的所有行號進行標記。
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("tr");
rows.forEach((row) => {
const z = document.createElement("td");
z.textContent = `(row #${row.rowIndex})`;
row.appendChild(z);
});
結果
規範
| 規範 |
|---|
| HTML # dom-tr-rowindex-dev |
瀏覽器相容性
載入中…