示例工程 19-table-accessibility

19-table-accessibility/demo-02-headers/index.html

html · 2.7 KB

运行:headers 演示

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>headers 演示</title>
    <style>
      body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.7; }
      .box { max-width: 760px; margin-bottom: 16px; padding: 18px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
      h1 { margin: 0 0 16px; font-size: 28px; }
      h2 { margin: 0 0 8px; font-size: 17px; }
      p { margin: 0 0 10px; color: #334155; }
      table { width: 100%; border-collapse: collapse; font-size: 14px; }
      caption { padding: 6px 8px; text-align: left; color: #475569; font-size: 13px; }
      th, td { padding: 8px 10px; border-bottom: 1px solid #e2e8f0; text-align: left; }
      thead th { background: #f1f5f9; }
      tbody th { background: #f8fafc; }
      td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
      pre { margin: 8px 0 0; padding: 12px; overflow-x: auto; border-radius: 8px; background: #0f172a; color: #e2e8f0; font-size: 12px; }
    </style>
  </head>
  <body>
    <h1>headers 演示</h1>

    <div class="box">
      <h2>两级表头</h2>
      <table>
        <caption>2026 年上半年月度数据</caption>
        <thead>
          <tr>
            <th id="month" rowspan="2" scope="col">月份</th>
            <th id="q1" colspan="2" scope="colgroup">Q1</th>
            <th id="q2" colspan="2" scope="colgroup">Q2</th>
          </tr>
          <tr>
            <th id="q1-visits" scope="col">访问</th>
            <th id="q1-conv" scope="col">转化</th>
            <th id="q2-visits" scope="col">访问</th>
            <th id="q2-conv" scope="col">转化</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <th id="jan" scope="row">1 月</th>
            <td headers="jan q1-visits">1200</td>
            <td headers="jan q1-conv">36</td>
            <td headers="jan q2-visits">1500</td>
            <td headers="jan q2-conv">45</td>
          </tr>
          <tr>
            <th id="feb" scope="row">2 月</th>
            <td headers="feb q1-visits">1300</td>
            <td headers="feb q1-conv">39</td>
            <td headers="feb q2-visits">1600</td>
            <td headers="feb q2-conv">48</td>
          </tr>
        </tbody>
      </table>
    </div>

    <div class="box">
      <h2>headers 写法</h2>
      <pre>&lt;th id="jan" scope="row"&gt;1 月&lt;/th&gt;
&lt;td headers="jan q1-visits"&gt;1200&lt;/td&gt;</pre>
      <p>每个 <code>headers</code> 要覆盖行名与所有列名,不能只写一个。</p>
    </div>
  </body>
</html>