大网数据广告活动
Web 标准与前端开发

HTMLTableCellElement:headers 属性

· 52CE 我爱测

HTMLTableCellElement 接口的 **`headers`** 属性包含一组为此特定单元格充当*表头*的 th 元素的 ID 列表。

HTML DOM

HTMLTableCellElement 接口的 headers 属性包含一组为此特定单元格充当表头的 th 元素的 ID 列表。

值

一个包含由空格分割的 ID 的字符串。

示例

此示例列出表格中最后一次被点击的单元格的 ID:

HTML

<table>
  <tr>
    <th rowspan="2" id="h">家庭作业(ID = h)</th>
    <th colspan="3" id="e">考试(ID = e)</th>
    <th colspan="3" id="p">项目(ID = p)</th>
  </tr>
  <tr>
    <th id="e1" headers="e">1(ID = e1)</th>
    <th id="e2" headers="e">2(ID = e2)</th>
    <th id="ef" headers="e">期末(ID = ef)</th>
    <th id="p1" headers="p">1(ID = p1)</th>
    <th id="p2" headers="p">2(ID = p2)</th>
    <th id="pf" headers="p">期末(ID = pf)</th>
  </tr>
  <tr>
    <td headers="h">15%</td>
    <td headers="e e1">15%</td>
    <td headers="e e2">15%</td>
    <td headers="e ef">20%</td>
    <td headers="p p1">10%</td>
    <td headers="p p2">10%</td>
    <td headers="p pf">15%</td>
  </tr>
</table>
最后一次点击的单元格的表头 ID:<output>无</output>。
table {
  border-collapse: collapse;
}

th,
td,
table {
  border: 1px solid black;
}

button {
  margin: 1em 1em 1em 0;
}

JavaScript

const table = document.querySelector("table");
const output = document.querySelector("output");

table.addEventListener("click", (ev) => {
  output.textContent = ev.target.headers ? ev.target.headers : "无";
});

结果

示例

规范

浏览器兼容性

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。