HTMLTableCellElement: headers 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

HTMLTableCellElement 介面的 headers 屬性包含一個 ID 列表,這些 ID 來自作為該單元格的表頭<th> 元素。

一個包含空格分隔 ID 的字串。

示例

此示例列出了表格中最後一個被點選單元格的 ID

HTML

html
<table>
  <tr>
    <th rowspan="2" id="h">Homework (ID = h)</th>
    <th colspan="3" id="e">Exams (ID = e)</th>
    <th colspan="3" id="p">Projects (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">Final (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">Final (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>
IDs of headers of the last clicked cell: <output>none</output>.

JavaScript

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

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

結果

規範

規範
HTML
# dom-tdth-headers

瀏覽器相容性