示例工程 36-aria-basics

36-aria-basics/demo-01-names/index.html

html · 1.9 KB

运行:ARIA 名称演示

点击「运行示例」后,左侧显示源码、右侧显示运行结果;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.icon { display: inline-flex; align-items: center; gap: 6px; padding: 7px; }
      button.icon svg { width: 18px; height: 18px; }
      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" 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>没有 <code>aria-label</code> 时,读屏只会读「按钮」。</p>
    </div>

    <div class="box">
      <h2>可见文字优先</h2>
      <button type="button">保存草稿</button>
      <p>按钮已有文字,不要再加 <code>aria-label</code> 覆盖。</p>
    </div>

    <div class="box">
      <h2>标题作为名称</h2>
      <section aria-labelledby="panel-title">
        <h2 id="panel-title">通知设置</h2>
        <p><code>aria-labelledby</code> 让区域名称与可见标题一致。</p>
      </section>
    </div>
  </body>
</html>