示例工程 19-table-accessibility
19-table-accessibility/demo-01-scope/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>scope 演示</title>
<style>
body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.7; }
.box { max-width: 720px; 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; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, monospace; }
</style>
</head>
<body>
<h1>scope 演示</h1>
<div class="box">
<h2>列名与行名</h2>
<table>
<caption>库存检查(件)</caption>
<thead>
<tr>
<th scope="col">仓库</th>
<th scope="col" class="num">可用</th>
<th scope="col" class="num">锁定</th>
</tr>
</thead>
<tbody>
<tr><th scope="row">上海</th><td class="num">120</td><td class="num">8</td></tr>
<tr><th scope="row">成都</th><td class="num">0</td><td class="num">—</td></tr>
<tr><th scope="row">广州</th><td class="num">64</td><td class="num">3</td></tr>
</tbody>
</table>
<p>读屏读某个数字时,会带上「上海,可用」这样的上下文。</p>
</div>
<div class="box">
<h2>长表头用 abbr</h2>
<table>
<caption>页面性能</caption>
<thead>
<tr>
<th scope="col">页面</th>
<th scope="col" class="num"><abbr title="Largest Contentful Paint">LCP</abbr></th>
<th scope="col" class="num"><abbr title="Cumulative Layout Shift">CLS</abbr></th>
</tr>
</thead>
<tbody>
<tr><th scope="row">首页</th><td class="num">1.2s</td><td class="num">0.02</td></tr>
<tr><th scope="row">文档</th><td class="num">1.8s</td><td class="num">0.01</td></tr>
</tbody>
</table>
</div>
</body>
</html>