示例工程 38-screen-reader
38-screen-reader/app/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>屏幕阅读器 · 名称、顺序与播报</title>
<meta name="description" content="从读屏用户视角检查标题、地标、链接、表单与表格。" />
<style>
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
padding: 30px 20px 40px;
background: #f8fafc;
color: #0f172a;
font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
line-height: 1.7;
}
.page { width: min(100%, 820px); margin: 0 auto; }
.skip { position: absolute; left: -9999px; top: 0; padding: 8px 12px; background: #2563eb; color: #fff; }
.skip:focus { left: 8px; top: 8px; z-index: 10; }
.site-header { display: flex; justify-content: space-between; gap: 16px; padding-bottom: 14px; border-bottom: 1px solid #e2e8f0; }
.brand { font-weight: 700; letter-spacing: 0.14em; font-size: 13px; }
nav a { color: #2563eb; font-size: 13px; text-decoration: none; }
nav a + a { margin-left: 14px; }
main { padding: 26px 0; }
.eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
h1 { margin: 6px 0 10px; font-size: clamp(30px, 6vw, 42px); line-height: 1.15; }
.lead { margin: 0 0 22px; color: #334155; font-size: 17px; }
section { margin-top: 26px; padding-top: 18px; border-top: 1px solid #e2e8f0; }
h2 { margin: 0 0 10px; font-size: 21px; }
h3 { margin: 16px 0 6px; font-size: 17px; }
p { margin: 0 0 12px; color: #334155; }
ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
li + li { margin-top: 4px; }
figure { margin: 14px 0; padding: 12px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
figure img { display: block; width: 100%; height: auto; border-radius: 6px; }
figcaption { margin-top: 6px; color: #64748b; font-size: 13px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; background: #fff; }
caption { padding: 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; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
form { padding: 14px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
label { display: block; margin: 6px 0 4px; font-size: 14px; }
input { width: 100%; max-width: 360px; padding: 7px 9px; border: 1px solid #cbd5e1; border-radius: 6px; font: inherit; }
button { padding: 7px 14px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; }
.error { margin: 4px 0 0; color: #b91c1c; font-size: 13px; }
.status { margin-top: 10px; padding: 8px 12px; border-left: 4px solid #16a34a; background: #f0fdf4; color: #14532d; font-size: 14px; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
footer { margin-top: 28px; padding-top: 14px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
@media (max-width: 560px) {
body { padding: 18px 14px 32px; }
.site-header { flex-direction: column; }
}
</style>
</head>
<body>
<a class="skip" href="#main">跳到主内容</a>
<div class="page">
<header class="site-header">
<span class="brand">A11Y / 2026</span>
<nav aria-label="页内导航">
<a href="#headings">标题</a>
<a href="#names">名称</a>
<a href="#tables">表格</a>
<a href="#forms">表单</a>
<a href="#testing">测试</a>
</nav>
</header>
<main id="main">
<p class="eyebrow">LESSON / 38</p>
<h1>屏幕阅读器</h1>
<p class="lead">
读屏用户不「看」页面,而是按标题、地标、链接、表单与表格导航。结构越清楚,任务越容易完成。
</p>
<section id="headings" aria-labelledby="headings-title">
<h2 id="headings-title">1. 标题与地标</h2>
<p>读屏可以列出所有标题,按层级跳转。</p>
<h3>标题层级</h3>
<p>一个 h1,分区用 h2,子主题用 h3,不跳级。</p>
<h3>地标</h3>
<p>header、nav、main、footer 让用户快速跳转。</p>
</section>
<section id="names" aria-labelledby="names-title">
<h2 id="names-title">2. 名称与顺序</h2>
<p>每个控件都要有可访问名称。</p>
<p>焦点顺序与 DOM 顺序一致,视觉重排不应打乱语义。</p>
<figure>
<img src="assets/chart.svg" alt="四个季度访问量柱状图,Q1 到 Q4 逐季上升" width="640" height="360" loading="lazy" />
<figcaption>图 38-1:季度访问量。</figcaption>
</figure>
</section>
<section id="tables" aria-labelledby="tables-title">
<h2 id="tables-title">3. 表格</h2>
<div style="overflow-x:auto">
<table>
<caption>2026 年季度访问与转化</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">Q1</th><td class="num">1200</td><td class="num">36</td></tr>
<tr><th scope="row">Q2</th><td class="num">1500</td><td class="num">45</td></tr>
<tr><th scope="row">Q3</th><td class="num">1800</td><td class="num">54</td></tr>
</tbody>
</table>
</div>
</section>
<section id="forms" aria-labelledby="forms-title">
<h2 id="forms-title">4. 表单与状态</h2>
<form action="#forms" method="get">
<label for="email">邮箱</label>
<input id="email" name="email" type="email" aria-invalid="true" aria-describedby="email-error" />
<p class="error" id="email-error">请输入有效的邮箱地址。</p>
<label for="name">称呼</label>
<input id="name" name="name" type="text" aria-describedby="name-help" />
<p id="name-help" style="color:#64748b;font-size:13px;margin:4px 0 0">用于邮件称呼。</p>
<p class="status" role="status" aria-live="polite">草稿已保存</p>
</form>
</section>
<section id="testing" aria-labelledby="testing-title">
<h2 id="testing-title">5. 测试方法</h2>
<ul>
<li>用读屏的「标题列表」检查层级。</li>
<li>用「地标列表」检查页面结构。</li>
<li>用「链接列表」检查链接文字。</li>
<li>用「表单列表」检查标签与错误。</li>
<li>用表格导航检查表头与单元格。</li>
</ul>
</section>
</main>
<footer>
<p>示例内容为虚构,仅用于教学。</p>
</footer>
</div>
</body>
</html>