游.程 2012-05-01 10:11 采纳率: 0%
浏览 172
已采纳

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条回答 默认 最新

  • 狐狸.fox 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条)

报告相同问题?

悬赏问题

  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用