1. 面向开发者的 Web 技术
  2. Web API
  3. HTMLTableRowElement
  4. HTMLTableRowElement:sectionRowIndex 属性

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

HTMLTableRowElement:sectionRowIndex 属性

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

HTMLTableRowElement 接口的 sectionRowIndex 只读属性表示当前部分(<thead><tbody><tfoot>)的行位置。

返回该行的索引,如果该行不属于表的一部分,则返回 -1

示例

本示例使用 JavaScript 标记表 tbody 中的所有行号。

HTML

html
<table>
 <thead>
 <tr>
 <th>商品</th>
 <th>价格</th>
 </tr>
 </thead>
 <tbody>
 <tr>
 <td>香蕉</td>
 <td>2ドル</td>
 </tr>
 <tr>
 <td>橘子</td>
 <td>8ドル</td>
 </tr>
 <tr>
 <td>西冷牛排</td>
 <td>20ドル</td>
 </tr>
 </tbody>
 <tfoot>
 <tr>
 <td>总计</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.sectionRowIndex})`;
 row.appendChild(z);
});

结果

规范

规范
HTML
# dom-tr-sectionrowindex

浏览器兼容性

参见

帮助改进 MDN

了解如何参与贡献

此页面最后更新于 ,由 MDN 贡献者更新。

AltStyle によって変換されたページ (->オリジナル) /