示例工程 36-aria-basics
36-aria-basics/demo-02-states/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>ARIA 状态演示</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; }
button { padding: 7px 12px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; }
button[aria-pressed="true"] { background: #1e40af; }
.panel { margin-top: 10px; padding: 10px; border: 1px solid #e2e8f0; border-radius: 8px; }
.panel[hidden] { display: none; }
.status { padding: 8px 10px; border-left: 4px solid #16a34a; background: #f0fdf4; color: #14532d; }
nav a { margin-right: 12px; color: #2563eb; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, monospace; }
</style>
</head>
<body>
<h1>状态</h1>
<div class="box">
<h2>展开 / 收起</h2>
<button type="button" aria-expanded="false" aria-controls="panel">展开详情</button>
<div class="panel" id="panel" hidden>详情内容。</div>
<p><code>aria-expanded</code> 表达状态,<code>aria-controls</code> 指向被控制区域。</p>
</div>
<div class="box">
<h2>开关</h2>
<button type="button" aria-pressed="false">静音</button>
<p>开关用 <code>aria-pressed</code>,按下后变为 true。</p>
</div>
<div class="box">
<h2>当前项</h2>
<nav aria-label="邮箱文件夹">
<a href="#inbox" aria-current="page">收件箱</a>
<a href="#sent">已发送</a>
</nav>
<p>当前页用 <code>aria-current="page"</code>。</p>
</div>
<div class="box">
<h2>实时状态</h2>
<p class="status" role="status" aria-live="polite">草稿已保存</p>
<p>异步状态用 <code>role="status"</code>,不要用 alert 播报普通提示。</p>
</div>
</body>
</html>