【问题标题】:How come some people use "role" attribute instead of "type"?为什么有些人使用“角色”属性而不是“类型”?
【发布时间】:2021-01-03 07:14:25
【问题描述】:

我正在关注一个 YouTube 视频,它正在教我有关按钮的知识。视频使用role属性,但他们怎么用role而不是type

<button type="button"></button>
<button role="button"></button>

我应该使用哪一个,为什么?我查过的所有 HTML 文档都没有将角色显示为按钮的属性。

【问题讨论】:

标签: html button element


【解决方案1】:

角色属性描述了一个元素在程序中的角色 可以使用它,例如屏幕阅读器或放大镜。

按钮角色应该用于触发 用户激活时的响应。添加 role="button" 将使 元素显示为屏幕阅读器的按钮控件。这个角色可以 与 aria-pressed 属性结合使用以创建 切换按钮。

https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/button_role阅读更多信息

因为您已经在使用 button 元素。它不再需要 role="button" 属性,因为它本身就是 button 并且所有事件都已经可用。

所以不建议在button上使用role="button"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2023-03-29
    • 2012-06-30
    相关资源
    最近更新 更多