示例工程 36-aria-basics

36-aria-basics/app/index.html

html · 6.3 KB

运行:ARIA 基础 · role / name / value

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>ARIA 基础 · role / name / value</title>
    <meta name="description" content="什么时候用 ARIA、如何命名、如何表达状态与实时区域。" />
    <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%, 800px); margin: 0 auto; }
      .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; }
      p { margin: 0 0 12px; color: #334155; }
      ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
      li + li { margin-top: 4px; }
      button { padding: 7px 12px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; font-size: 14px; cursor: pointer; }
      button[aria-pressed="true"] { background: #1e40af; }
      button.icon { padding: 7px; display: inline-flex; align-items: center; gap: 6px; }
      button.icon svg { width: 18px; height: 18px; }
      .panel { margin-top: 10px; padding: 12px; border: 1px solid #e2e8f0; border-radius: 8px; background: #fff; }
      .panel[hidden] { display: none; }
      .status { padding: 8px 12px; border-left: 4px solid #16a34a; border-radius: 6px; background: #f0fdf4; color: #14532d; font-size: 14px; }
      .decoration { color: #64748b; font-size: 13px; }
      .visually-hidden { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
      code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
      .note { padding: 12px 14px; border-left: 4px solid #2563eb; border-radius: 6px; background: #eff6ff; color: #1e3a8a; font-size: 14px; }
      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>
    <div class="page">
      <header class="site-header">
        <span class="brand">A11Y / 2026</span>
        <nav aria-label="页内导航">
          <a href="#names">名称</a>
          <a href="#states">状态</a>
          <a href="#live">实时区域</a>
          <a href="#rules">规则</a>
        </nav>
      </header>

      <main>
        <p class="eyebrow">LESSON / 36</p>
        <h1>ARIA 基础</h1>
        <p class="lead">
          ARIA 补的是语义,不是行为。能用原生元素时,优先原生;需要时,再补角色、名称与状态。
        </p>

        <section id="names">
          <h2>1. 名称</h2>

          <p>图标按钮需要可访问名称:</p>
          <button type="button" class="icon" aria-label="关闭对话框">
            <svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
              <path d="M6 6 L18 18 M18 6 L6 18" stroke="currentColor" stroke-width="2" fill="none" />
            </svg>
          </button>

          <p>可见文字已经足够时,不要再加 `aria-label` 覆盖它:</p>
          <button type="button">保存草稿</button>

          <p>区域名称可以来自可见标题:</p>
          <section aria-labelledby="notify-title">
            <h3 id="notify-title">通知设置</h3>
            <p>这里用 <code>aria-labelledby</code> 指向标题。</p>
          </section>

          <p>装饰图标对读屏隐藏:</p>
          <p class="decoration"><span aria-hidden="true">★</span> 仅作装饰的星号</p>
        </section>

        <section id="states">
          <h2>2. 状态</h2>

          <p>展开/收起用 <code>aria-expanded</code> 与 <code>aria-controls</code>:</p>
          <button type="button" aria-expanded="false" aria-controls="details-panel">展开详情</button>
          <div class="panel" id="details-panel" hidden>这里是详情内容。</div>

          <p>开关用 <code>aria-pressed</code>:</p>
          <button type="button" aria-pressed="false">静音</button>

          <p>当前页用 <code>aria-current</code>:</p>
          <nav aria-label="邮箱文件夹">
            <a href="#inbox" aria-current="page">收件箱</a>
            <a href="#sent">已发送</a>
          </nav>
        </section>

        <section id="live">
          <h2>3. 实时区域</h2>
          <p><code>role="status"</code> 会在内容变化时礼貌播报:</p>
          <p class="status" role="status" aria-live="polite">草稿已保存</p>
          <p>紧急错误用 <code>role="alert"</code>,但不要滥用。</p>
        </section>

        <section id="rules">
          <h2>4. 规则</h2>
          <ul>
            <li>能用原生元素就用原生,不要用 <code>div role="button"</code>。</li>
            <li>每个交互元素都要有可访问名称。</li>
            <li>可见文字足够时,不要用 <code>aria-label</code> 覆盖。</li>
            <li>状态要随交互更新,不能只写一次。</li>
            <li>装饰内容用 <code>aria-hidden="true"</code>。</li>
            <li>实时区域只用于异步状态,不要滥用。</li>
          </ul>
          <p class="note">ARIA 不添加键盘行为与焦点管理,这些仍要自己实现或用原生元素。</p>
        </section>
      </main>

      <footer>
        <p>示例内容为虚构,仅用于教学。</p>
      </footer>
    </div>
  </body>
</html>