ARIA: rowheader 角色

具有 role="rowheader" 的元素是 單元格,其中包含在 gridtabletreegrid 的表格結構中,某 的標題資訊。

描述

Rowheader 是行的標題 單元格,它在行內的其他單元格之間建立關係。

html
<div
  role="table"
  aria-label="Populations"
  aria-describedby="country_population_desc">
  <div id="country_population_desc">World Populations by Country</div>
  <div role="rowgroup">
    <div role="row">
      <span role="columnheader" aria-sort="descending">Country</span>
      <span role="columnheader" aria-sort="none">Population</span>
    </div>
  </div>
  <div role="rowgroup">
    <div role="row">
      <span role="rowheader">Finland</span>
      <span role="cell">5.5 million</span>
    </div>
    <div role="row">
      <span role="rowheader">France</span>
      <span role="cell">67 million</span>
    </div>
  </div>
</div>

它在功能上等同於 `scope` 為 `row` 的 <th> 元素,即 `<th scope="row">。強烈建議使用原生的 <th> HTML 元素。

要建立一個 ARIA 行標題,請將 role="rowheader" 新增到元素中。該行標題必須巢狀在 row 中,而 row 又巢狀在 rowgroup 中,或者直接巢狀在 gridtabletreegrid 中。

注意: 強烈建議儘可能使用原生的 表格元素

關聯的 WAI-ARIA 角色、狀態和屬性

上下文角色

role="row"

這是您會發現行的唯一上下文。它包含一個單元格或一組單元格行,其中只有一個應該是行標題型別。類似於原生的 <tr> HTML 元素。

鍵盤互動

無。

所需的 JavaScript 功能

無。

注意: ARIA 使用的第一條規則是,如果您可以使用一個具有您所需語義和行為的原生功能,而不是重新使用一個元素並新增 ARIA 角色、狀態或屬性使其可訪問,那麼就應該這樣做。儘可能使用 HTML <table><tr><th><td> 和其他 表格元素,而不是 ARIA 表格角色。

示例

html
<div
  role="table"
  aria-label="Semantic Elements"
  aria-describedby="semantic_elements_table_desc"
  aria-rowcount="81">
  <div id="semantic_elements_table_desc">
    Semantic Elements to use instead of ARIA's roles
  </div>
  <div role="rowgroup">
    <div role="row">
      <span role="columnheader" aria-sort="none">ARIA Role</span>
      <span role="columnheader" aria-sort="none">Semantic Element</span>
    </div>
  </div>
  <div role="rowgroup">
    <div role="row" aria-rowindex="11">
      <span role="rowheader">header</span>
      <span role="cell">h1</span>
    </div>
    <div role="row" aria-rowindex="16">
      <span role="rowheader">header</span>
      <span role="cell">h6</span>
    </div>
    <div role="row" aria-rowindex="18">
      <span role="rowheader">rowgroup</span>
      <span role="cell">thead</span>
    </div>
    <div role="row" aria-rowindex="24">
      <span role="rowheader">term</span>
      <span role="cell">dt</span>
    </div>
  </div>
</div>

上面是一個非語義的 ARIA 表格,包含表頭和表體,DOM 中有 81 行中的 5 行:一行在表頭內,四行在表體中。標題行獨自在一個行組中,有兩個列標題。列是可排序的,但當前未排序,如 aria-sort 屬性所示。表體是一個單獨的行組,當前 DOM 中有四行。每個資料行都有一個行標題。由於並非所有行都在 DOM 中,我們在每個行上都添加了 aria-rowindex 屬性。

最佳實踐

僅將 <table><tbody><thead><tr><th><td> 等用於資料表格結構。您可以在原生表格語義被移除(例如使用 CSS)時新增這些 ARIA 角色以確保可訪問性。對於所有 ARIA 表格角色,一個相關的用例是當 CSS 的 display 屬性覆蓋了表格的原生語義時,例如使用 display: grid。在這種情況下,您可以使用 ARIA 表格角色來新增語義。

html
<table
  role="table"
  aria-label="Semantic Elements"
  aria-describedby="semantic_elements_table_desc"
  aria-rowcount="81">
  <caption id="semantic_elements_table_desc">
    Semantic Elements to use instead of ARIA's roles
  </caption>
  <thead role="rowgroup">
    <tr role="row">
      <th role="columnheader" aria-sort="none">ARIA Role</th>
      <th role="columnheader" aria-sort="none">Semantic Element</th>
    </tr>
  </thead>
  <tbody role="rowgroup">
    <tr role="row" aria-rowindex="11">
      <th role="rowheader">header</th>
      <td role="cell">h1</td>
    </tr>
    <tr role="row" aria-rowindex="16">
      <th role="rowheader">header</th>
      <td role="cell">h6</td>
    </tr>
  </tbody>
</table>

以上是編寫表格的語義化方式。僅當表格的原生語義(以及因此的表格行標題)被破壞時(例如透過將 display 屬性設定為 flex 或 grid),才需要 ARIA 角色。

新增優勢

none

規範

規範
無障礙富網際網路應用程式 (WAI-ARIA)
# rowheader

另見