'role' 属性在 HTML5 中是必需的吗?
Are 'role' attributes necessary in HTML5?
为了跟上 HTML5,我确保使用 W3 validator tool 来验证我的所有标记。最近,我开始给元素添加 role
属性。但是,我注意到以下警告一次又一次地弹出:
The button
role is unnecessary for element button
.
The navigation
role is unnecessary for element nav
.
但同理,在W3C网站上关于ARIA in HTML,它指出button
role is applicable to the <button>
element and that the navigation
role 仅适用于<nav>
元素.
这让我想到了我的问题,HTML5 中的角色是否需要 role
属性?我会有什么场景需要使用它们?
If you can use a native HTML element [HTML51] or attribute with the
semantics and behaviour you require already built in, instead of
re-purposing an element and adding an ARIA role, state or property to
make it accessible, then do so.
如果你查看navigation role in WAI-ARIA, it states that HTML Nav是一个相关的概念,而navigation=role
已经是默认的角色,那么就不用自己手动设置了:
Allowed ARIA role attribute values:
navigation role (default - do not set).
所以导航角色是适用于<nav>
,但它已经默认设置,所以你不需要自己做。
总结一下您的问题,不,如果角色已经隐含在 HTML5 的语义中,请不要设置角色。你可以查看下面的网站,我认为它概述了一些关于你何时 should/shouldn 不使用 ARIA 的要点(它不只是关于角色属性,但它也涵盖了这一点):
What is WAI-ARIA, what does it do for me, and what not?
为了跟上 HTML5,我确保使用 W3 validator tool 来验证我的所有标记。最近,我开始给元素添加 role
属性。但是,我注意到以下警告一次又一次地弹出:
The
button
role is unnecessary for elementbutton
.The
navigation
role is unnecessary for elementnav
.
但同理,在W3C网站上关于ARIA in HTML,它指出button
role is applicable to the <button>
element and that the navigation
role 仅适用于<nav>
元素.
这让我想到了我的问题,HTML5 中的角色是否需要 role
属性?我会有什么场景需要使用它们?
If you can use a native HTML element [HTML51] or attribute with the semantics and behaviour you require already built in, instead of re-purposing an element and adding an ARIA role, state or property to make it accessible, then do so.
如果你查看navigation role in WAI-ARIA, it states that HTML Nav是一个相关的概念,而navigation=role
已经是默认的角色,那么就不用自己手动设置了:
Allowed ARIA role attribute values: navigation role (default - do not set).
所以导航角色是适用于<nav>
,但它已经默认设置,所以你不需要自己做。
总结一下您的问题,不,如果角色已经隐含在 HTML5 的语义中,请不要设置角色。你可以查看下面的网站,我认为它概述了一些关于你何时 should/shouldn 不使用 ARIA 的要点(它不只是关于角色属性,但它也涵盖了这一点):
What is WAI-ARIA, what does it do for me, and what not?