Web可访问性与WCAG合规 accessibility-wcag

这个技能专注于通过使用语义HTML、ARIA属性、键盘导航、表单优化、颜色对比等技术,确保网站和应用程序符合WCAG 2.2标准,提高可访问性,方便残障用户(如视障、听障、运动障碍)使用。关键词:Web可访问性、WCAG、ARIA、键盘导航、屏幕阅读器、HTML、CSS、JavaScript、前端开发、用户体验。

前端开发 0 次安装 0 次浏览 更新于 3/8/2026

名称: accessibility-wcag 描述: 用于WCAG 2.2合规的Web可访问性模式,包括ARIA、键盘导航、屏幕阅读器和测试

可访问性与WCAG

语义HTML

<!-- 使用语义元素而非通用divs -->
<header>
  <nav aria-label="主导航">
    <ul>
      <li><a href="/" aria-current="page">首页</a></li>
      <li><a href="/products">产品</a></li>
      <li><a href="/about">关于</a></li>
    </ul>
  </nav>
</header>

<main>
  <article>
    <h1>产品详情</h1>
    <section aria-labelledby="specs-heading">
      <h2 id="specs-heading">规格</h2>
      <dl>
        <dt>重量</dt>
        <dd>1.2 kg</dd>
        <dt>尺寸</dt>
        <dd>30 x 20 x 10 cm</dd>
      </dl>
    </section>
  </article>
</main>

<footer>
  <p>&copy; 2024 公司名称</p>
</footer>

使用 <nav><main><article><section><aside> 而不是 <div> 作为地标元素。屏幕阅读器使用这些来导航页面。

ARIA模式

function Modal({ isOpen, onClose, title, children }) {
  if (!isOpen) return null;

  return (
    <div
      role="dialog"
      aria-modal="true"
      aria-labelledby="modal-title"
      onKeyDown={(e) => e.key === "Escape" && onClose()}
    >
      <h2 id="modal-title">{title}</h2>
      <div>{children}</div>
      <button onClick={onClose} aria-label="关闭对话框">
        <XIcon aria-hidden="true" />
      </button>
    </div>
  );
}

function Tabs({ tabs, activeIndex, onChange }) {
  return (
    <div>
      <div role="tablist" aria-label="设置部分">
        {tabs.map((tab, i) => (
          <button
            key={tab.id}
            role="tab"
            id={`tab-${tab.id}`}
            aria-selected={i === activeIndex}
            aria-controls={`panel-${tab.id}`}
            tabIndex={i === activeIndex ? 0 : -1}
            onClick={() => onChange(i)}
            onKeyDown={(e) => handleArrowKeys(e, i, tabs.length, onChange)}
          >
            {tab.label}
          </button>
        ))}
      </div>
      {tabs.map((tab, i) => (
        <div
          key={tab.id}
          role="tabpanel"
          id={`panel-${tab.id}`}
          aria-labelledby={`tab-${tab.id}`}
          hidden={i !== activeIndex}
          tabIndex={0}
        >
          {tab.content}
        </div>
      ))}
    </div>
  );
}

键盘导航

function handleArrowKeys(
  event: React.KeyboardEvent,
  currentIndex: number,
  totalItems: number,
  onSelect: (index: number) => void
) {
  let newIndex = currentIndex;

  switch (event.key) {
    case "ArrowRight":
    case "ArrowDown":
      newIndex = (currentIndex + 1) % totalItems;
      break;
    case "ArrowLeft":
    case "ArrowUp":
      newIndex = (currentIndex - 1 + totalItems) % totalItems;
      break;
    case "Home":
      newIndex = 0;
      break;
    case "End":
      newIndex = totalItems - 1;
      break;
    default:
      return;
  }

  event.preventDefault();
  onSelect(newIndex);
}

所有交互元素必须可通过键盘访问。Tab用于焦点导航,Enter/Space用于激活,方向键用于组件内导航。

表单可访问性

function SignupForm() {
  return (
    <form aria-labelledby="form-title" noValidate>
      <h2 id="form-title">创建账户</h2>

      <div>
        <label htmlFor="email">邮箱地址</label>
        <input
          id="email"
          type="email"
          required
          aria-required="true"
          aria-describedby="email-hint email-error"
          aria-invalid={hasError ? "true" : undefined}
        />
        <p id="email-hint">我们绝不会分享您的邮箱。</p>
        {hasError && (
          <p id="email-error" role="alert">
            请输入有效的邮箱地址。
          </p>
        )}
      </div>

      <button type="submit">创建账户</button>
    </form>
  );
}

颜色与对比

:root {
  --text-primary: #1a1a1a;      /* 15.3:1 在白色背景上 */
  --text-secondary: #595959;    /* 7.0:1 在白色背景上 */
  --text-on-primary: #ffffff;   /* 确保在品牌颜色上有4.5:1对比度 */
  --border-focus: #0066cc;      /* 可见焦点环 */
}

*:focus-visible {
  outline: 3px solid var(--border-focus);
  outline-offset: 2px;
}

.error-message {
  color: #d32f2f;
  /* 不要仅依赖颜色 - 添加图标或文本前缀 */
}
.error-message::before {
  content: "错误: ";
  font-weight: bold;
}

WCAG AA要求普通文本对比度4.5:1,大文本(18px粗体或24px常规)3:1。

反模式

  • 使用 divspan 作为可点击元素,而不是 buttona
  • 移除焦点轮廓而不提供替代指示器
  • 仅依赖颜色传达信息(如红色表示错误,绿色表示成功)
  • 当可见文本已标记元素时使用 aria-label
  • 无暂停机制自动播放媒体
  • 键盘用户缺少跳转导航链接

清单

  • [ ] 所有交互元素键盘可访问(Tab、Enter、Escape、方向键)
  • [ ] 使用语义HTML地标元素(navmainarticlesection
  • [ ] 图像具有描述性 alt 文本(或装饰性图片使用 alt=""
  • [ ] 颜色对比满足WCAG AA(普通文本4.5:1,大文本3:1)
  • [ ] 所有交互元素上可见焦点指示器
  • [ ] 表单输入具有关联的 <label> 元素
  • [ ] 错误消息通过 role="alert" 向屏幕阅读器宣布
  • [ ] 页面用屏幕阅读器(VoiceOver、NVDA)和仅键盘测试