我是否需要将``onkeydown''添加到`button''以符合可访问性? (WCAG 2.0)

发布于 2025-01-22 03:39:37 字数 911 浏览 3 评论 0原文

我有一个按钮,比单击时会展开一张带有更多信息的卡,

我需要将onkeydown添加到按钮以符合访问性吗?

还是因为元素已经是按钮,所以这是多余的?

我缺少其他符合WCAG 2.0的东西?

这是我带有react

const ariaPressed = checked ? "true" : "false";

return (
  <button
    tabIndex={0}
    role="button"
    component="button"
    aria-pressed={ariaPressed}
    aria-expanded={ariaPressed}
    onClick={toggleChecked}
  >
    {checked ? "Hide Versions" : "View Versions"}
  </button>
);

”在此处输入图像说明”

I have a button than when clicked will expand a card with more information

Do I need to add onKeyDown to button for accessibility compliance?

Or is that redundant because the element is already a button?

I am missing something else to comply with WCAG 2.0?

Here is my collapsible button code with react

const ariaPressed = checked ? "true" : "false";

return (
  <button
    tabIndex={0}
    role="button"
    component="button"
    aria-pressed={ariaPressed}
    aria-expanded={ariaPressed}
    onClick={toggleChecked}
  >
    {checked ? "Hide Versions" : "View Versions"}
  </button>
);

enter image description here
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流殇 2025-01-29 03:39:37

不,您不需要onkeydown,因为您使用的是本机&lt; button&gt;。一个按钮将驱动onclick()无论您单击鼠标,按 Enter space 带键盘,还是在移动设备上点击。

附带说明,您也不需要tabindex = 0,因为&lt; button&gt; s本地键盘可集中。

而且您不需要roun = button,因为这是本机&lt; button&gt;的默认角色。

由于您正在使用ARIA扩展,因此我还会摆脱ARIA-PARDESS。只要您在“ true”和“ false”之间切换ARIA扩展的值,这就是您所需要的。

No, you don't need onKeyDown since you're using a native <button>. A button will drive the onClick() whether you click with a mouse, press ENTER or SPACE with the keyboard, or tap on a mobile device.

As a side note, you also don't need tabindex=0 since <button>s are natively keyboard focusable.

And you don't need role=button since that's the default role for a native <button>.

I would also get rid of aria-pressed since you're using aria-expanded. As long as you toggle the value of aria-expanded between "true" and "false", that's all you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文