Html 为什么在 html5 中使用 role 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25264371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:29:00  来源:igfitidea点击:

why use role attribute in html5

htmlcsstwitter-bootstrap

提问by olive_tree

I have created a web page using html5.

我已经使用 html5 创建了一个网页。

I used source code from bootstrap.

我使用了 bootstrap 的源代码。

For example: role="main", role ="navigation", role = "document"etc.

例如:role="main"role ="navigation"role = "document"等。

If I remove these attribute from my code, page didn't any change.

如果我从我的代码中删除这些属性,页面没有任何变化。

So I want to know: what is the purpose of "role" attribute?

所以我想知道:“角色”属性的目的是什么?

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link href='http://fonts.googleapis.com/css?family=Raleway:900,500,600,200,400,700' rel='stylesheet' type='text/css'>
      <title>Layout</title>
      <!-- Bootstrap core CSS -->
      <link href="css/bootstrap.min.css" rel="stylesheet">
      <link href="css/style.css" rel="stylesheet">
   </head>
   <body role="document">
      <!-- Fixed navbar -->
      <div class="navbar" role="navigation">
         <div class="container">
            <div class="navbar-header">
               <h1><img src="img/logo.png" /></h1>
            </div>
         </div>
      </div>
      <div class="container theme-showcase" role="main">
         <!-- Main jumbotron for a primary marketing message or call to action -->
         <div class="container">
            <div class="banner-div">
               <img src="img/banner.png" class="banner"/>
               <img src="img/issue-no-img.png" class="issue-badge"/>
               <span class="issue">Issue No.<br /><b>376</b></span>
            </div>
            <div class="row">
               <div class="col-md-4">
                  <img src="img/left-note-img.png"/>
               </div>
               <div class="col-md-4 align-center">
                  <p><span class="new-radius"> NEW! </span>&nbsp;<span class="new">JUN 07, 2003</span></p>
                  <p><i>Get your breaks points on.</i></p>
                  <span>
                     <h1>DOT NET ARTICLES</h1>
                  </span>

                  <span class="comments"><i>by</i> <a>JOHN WOO</a> <i> - 10 Commments</i></span><br/>
               </div>
               <div class="col-md-4 pull-right">
                  <div class="input-group">
                     <input type="text" class="form-control search-input-box" placeholder="Search..." />
                     <span class="input-group-addon glyphicon glyphicon-search"></span>
                  </div>
               </div>
            </div>
            <hr class="hr-style"/>
            <p class = "header-pgf">
               Text, navigation, and tables, oh, my! What's a responsive web designer to do? How can you confine your design to as few major breakpoints as possible? Where and when will you sketching?  Is it possible to sketch on actual devices, and what are the accessibility implications of doing so? The answers to these and other profound questions will be found in this exclusive excerpt from Chapter 7 of Responsive Design Workflow, Stephen Hay's new book, available now from New Riders.
            </p>
            <hr class="hr-style">
            <h1 class="more-apart">More from A List Apart</h1>
            <hr class="hr-style">
            <div class="row">
               <div class="col-md-3">
                  <h2>Columnists</h2>
                  <p>Hyman MCGRANE <i>on</i> CONTENT</p>
                  <p class="pgf-header">The Alternative is Nothing</p>
                  <img src="img/thumb1.jpg" class="float-left" /> 
                  <p>We're witnessing one of thr latest waves of technological disruption, as mobile devices put access to the internet in the hands of people who previously never had that power.</p>
               </div>
               <div class="col-md-3">
                  <h2>From the Blog</h2>
                  <p class="pgf-header">Maps Should Be Crafted.Not "Plugged In"></p>
                  <p>Web designers: erase the line between "the map" and "the content" by harnessing the power of open-source Leafler and your own fresh creative thinking. In the tradition of ALA's recent "Hack Your Maps." Happy Cog's Brandon Rosage shares how to make location a central aspect of the content experience-not just a visual aid.</p>
               </div>
               <div class="col-md-3">
                  <p class="md-3">Amazon Web Services Introduces a New API</p>
                  <p>Amazon Web Services Identity and Access Management(IAM) is expanding to support web identity federation. Developers can integrate Amazon.com, Facebook, or Google odentity into their app by using the new AWS Security Token Services(STS) API, AssumeRoleWithWebIdentity. to request temporary security credentials.</p>
               </div>
               <div class="col-md-3">
                  <h5>Gratitude</h5>
                  <p>Thanks to our RSS sponser Typekit-offering desktop and web fonts in a single subscription starting June 17</p>
                  <div class="well align-center">
                     <img src="img/mothers-day.jpg" /> <br/>
                     <p class="font-10">Two special Mothers' Say Kits are available from Field Notes Brand. Ad via The Deck </p>
                  </div>
                  <div class="job-board">
                     <h5>Job Board</h5>
                     <a href="#" >New York Times is looking for a <br />Ruby on Rails Web Developer.</a>
                     More on the <a href ="#">Job Board ></a>
                  </div>
               </div>
            </div>
            <br />
         </div>
      </div>
      <!-- /container -->
      <div class="footer" style="">
         <div class="container">
            <div class="list-footer" style="font-size:13px;">
               <span class="padding-10"><img src="img/article-logo.png" /></span>
               <span class="padding-10">ARTICLES</span>
               <span class="padding-10">COLUMNS</span>
               <span class="padding-10">BLOG</span>
               <span class="padding-10">TOPICS</span>
            </div>
            <div class = "list-footer" style="font-size:12px;"> 
               <span class="padding-10">ABOUT</span>
               <span class="padding-10">AUTHORS</span>
               <span class="padding-10">MASTHEAD</span>
               <span class="padding-10">CONTRIBUTE</span>
               <span class="padding-10">STYLE GUIDE</span>
               <span class="padding-10">CONTACT</span>
               <span class="padding-10">SPONSORSHIPS</span>
            </div>
            <hr class="hr-style"/>

            <div class="row">
               <div class="col-md-6">
                  <img src="img/dot-net-ad.jpg" class="footer-image"/> 
                  <h3>.NET Training</h3>
                  <p class="footer-pgf">If you have a .NET question on a topic that 's not covered by other more specific forums.</p>
                  <a class="footer-link" href="#">ask here. ></a> 
               </div>
               <div class="col-md-6">
                  <img src="img/shopify-expert-ad.jpg" class="footer-image"/> 
                  <h3>Shopify Expert</h3>
                  <p class="footer-pgf">Unique custom made Shopify theme and tweaks.</p>
                  <a class="footer-link" href="#">click to view</a>
               </div>
            </div>
            <hr class="hr-style" />
            <div style="text-align:center">
               <p class="copyright">Copyright &copy; 2013 Dot Net How</p>
            </div>
         </div>
      </div>
   </body>
</html>

回答by olive_tree

It provides support for ARIA (Accessible Rich Internet Applications) which allows to specify even more semantic richness in documents.

它提供对 ARIA(可访问的富互联网应用程序)的支持,允许在文档中指定更多的语义丰富性。

You can add role="search" to your search form, role="banner" to your masthead, and role="contentinfo" to your page footer. There's a full list of values in the ARIA specification at http://www.w3.org/TR/wai-aria/roles#role_definitions.

您可以将 role="search" 添加到搜索表单,将 role="banner" 添加到标头,将 role="contentinfo" 添加到页脚。在http://www.w3.org/TR/wai-aria/roles#role_definitions 上有 ARIA 规范中的完整值列表 。

Basically you don't have to add them, but its better if you do as it provides more context for your page. More discussed at A List Apart.

基本上你不必添加它们,但如果你这样做会更好,因为它为你的页面提供了更多的上下文。更多讨论在A List Apart。

回答by Kheema Pandey

The new structural elements in HTML5 will be very useful to assistive technology. Instead of creating “skip navigation” links, all we need to do is use the nav element correctly. This will allow screen reader users to skip past navigation without us having to provide an explicit link.

HTML5 中的新结构元素将对辅助技术非常有用。我们需要做的不是创建“跳过导航”链接,而是正确使用导航元素。这将允许屏幕阅读器用户跳过导航而无需我们提供明确的链接。

Twitter Bootstrap uses like <nav role="navigation">. So Bootstrap take consider not only normal browser but also take care of screen reader browsers.

Twitter Bootstrap 使用像<nav role="navigation">. 所以 Bootstrap 不仅要考虑普通浏览器,还要考虑屏幕阅读器浏览器。

Note:By including Roleattribute you are making your website more accessible and its good practice to use this Roleattribute.

注意:通过包含Role属性,您可以使您的网站更易于访问,并且是使用此Role属性的良好做法。