~Onlooker 2012-05-01 10:11 采纳率: 0%
浏览 291
已采纳

Html 中"角色"属性的目的是什么?

I keep seeing role attributes in some people's work. I use it too, but I'm not sure about its effect.

For example:

<header id="header" role="banner">
    Header stuff in here
</header>

Or:

<section id="facebook" role="contentinfo">
    Facebook stuff in here
</section>

Or:

<section id="main" role="main">
     Main content stuff in here
</section>

Is this role attribute necessary?

Is this attribute better for semantics?

Does it improve SEO?

A list of roles can be found here, but I see some people make up their own. Is that allowed or a correct use of the role attribute?

Any thoughts on this?

转载于:https://stackoverflow.com/questions/10403138/what-is-the-purpose-of-the-role-attribute-in-html

  • 写回答

4条回答 默认 最新

  • Memor.の 2013-09-06 18:12
    关注

    Most of the roles you see were defined as part of ARIA 1.0, and then later incorporated into HTML5. Some of the new HTML5 elements (dialog, main, etc.) are even based on the original ARIA roles.

    http://www.w3.org/TR/wai-aria/

    There are two primary reasons to use roles in addition to your native semantic element.

    Reason #1. Overriding the role where no host language element is appropriate or, for various reasons, a less semantically appropriate element was used.

    In this example, a link was used, even though the resulting functionality is more button-like than a navigation link.

    <a href="#" role="button" aria-label="Delete item 1">Delete</a>
    

    Screen readers will hear this as a button (as opposed to a link), and you can use a CSS attribute selector to avoid class-itis and div-itis.

    *[role="button"] {
      /* style these a buttons w/o relying on a .button class */
    }
    

    Reason #2. Backing up a native element's role, to support browsers that implemented the ARIA role but haven't yet implemented the native element's role.

    For example, the "main" role has been supported in browsers for many years, but it's a relatively recent addition to HTML5, so many browsers don't yet support the semantic for <main>.

    <main role="main">…</main>
    

    This is technically redundant, but helps some users and doesn't harm any. In a few years, this technique will likely become unnecessary.

    You also wrote:

    I see some people make up their own. Is that allowed or a correct use of the role attribute?

    That's a valid use of the attribute unless a real role is not included. Browsers will apply the first recognized role in the token list.

    <span role="foo link note bar">...</a>
    

    Out of the list, only link and note are valid roles, and so the link role will be applied because it comes first. If you use custom roles, make sure they don't conflict with any defined role in ARIA or the host language you're using (HTML, SVG, MathML, etc.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题