简体   繁体   English

这是使用html角色的正确方法吗?

[英]Is this the correct way to use html role?

As I have seen that the role attribute is fully supported in HTML5 semantics doctype. 如我所见,HTML5语义doctype中完全支持role属性。 I have used it in JQuery selectors such as: 我在诸如以下的JQuery选择器中使用了它:

<div role="inputForm">
  <div role="field">
    Name : <input type="text" role="name" />
  </div>
  <div role="field">
    Address : <textarea type="text" role="address"></textarea>
  </div>
  <div role="field">
    Country : <select role="country"></select>
  </div>
</div>

<script>
var inputForm = $("div[role='inputForm']");
var fields = $(inputForm.find("div[role='field']"));
var nameField = $(inputForm.find("input[role='name']"));
</script>

As I have known, the same attribute is being used in JqGrid , grid plugin for JQuery . 正如我所已知的,同样的属性正在使用中JqGrid ,网格插件JQuery

I think that this selector is more efficient and modular than class / id based selector. 我认为这个选择器比基于类/ id的选择器更有效和模块化。 However, is this the correct use of role attribute in HTML5? 但是,这是在HTML5中正确使用角色属性吗? Or is there any risk of doing this design? 还是有进行这种设计的风险?

If this kind of design does not supported / recommended as HTML best practice, is there any way to achieve the same? 如果这种设计不支持/不建议作为HTML最佳实践,是否有任何方法可以实现相同目的?

I cannot find any articles or supported documents for this kind of usage. 我找不到此类用法的任何文章或支持的文档。

It appears you should be using id instead of role (though not for your role="field" attributes... those should be classes). 看来您应该使用id而不是角色(尽管不是您的role="field"属性的属性……这些应该是类)。

It short, your role attribute values are not being used as intended per the WAI-ARIA specification. 简而言之,您的角色属性值未按照WAI-ARIA规范的预期使用。

The role attribute should be used to assign semantic meaning to document elements, and there are a limited set of valid and appropriate values defined by the Accessible Rich Internet Applications Suite (ARIA), to make Web content and Web applications more accessible. 应该使用role属性为文档元素分配语义,并且Accessible Rich Internet Applications Suite(ARIA)定义了一组有限的有效值和适当值,以使Web内容和Web应用程序更易于访问。

An extensive list of the valid role attribute values can be found at the W3C spec documentation for The Roles Model: http://www.w3.org/TR/wai-aria/roles#abstract_roles 有关有效角色属性值的详细列表,请参见W3C规范文档中的“角色模型”: http : //www.w3.org/TR/wai-aria/roles#abstract_roles

No, it is not. 不它不是。 HTML5 defines : HTML5 定义

Authors may use the ARIA role and aria-* attributes on HTML elements, in accordance with the requirements described in the ARIA specifications, except where these conflict with the strong native semantics described below. 根据ARIA规范中所述的要求,作者可以在HTML元素上使用ARIA rolearia-*属性,除非这些条件与下文所述的强本机语义冲突。

Your values inputForm and field are not valid . 您的值inputFormfield 无效

If you use the role attribute correctly, you can of course use it as a hook in CSS and JS, too. 如果正确使用了role属性,那么当然也可以将其用作CSS和JS中的钩子。


I think that this selector is more efficient and modular than class / id based selector. 我认为这个选择器比基于类/ id的选择器更有效和模块化。

Your actual intention seems to be to find an identifier that can be used on several (third party) sites, without the risk of name collisions . 您的实际意图似乎是找到一个可以在多个(第三方)站点上使用的标识符,而不会造成名称冲突

You could use the class , the id , or the data-* attributes . 您可以使用classiddata-*属性 To make collisions unlikely, you could use a "rare" prefix, for example your domain name ( example.com ): 为了避免发生冲突,您可以使用“稀有”前缀,例如您的域名( example.com ):

<div class="com-example-foo"></div> 
<div id="com-example-foo"></div> 
<div data-com-example="foo"></div> <!-- preferred, because the prefix is not part of the value -->

Another possibility would be to use a custom vocabulary with Microdata or RDFa . 另一种可能性是将自定义词汇表与Microdata或RDFa一起使用 As you can use your own URIs as value, it's very unlikely that anyone else would use these (unless you publish your vocabulary and it's useful for others ;-)). 因为您可以使用自己的URI作为值,所以其他任何人都不太可能使用它们(除非您发布词汇表,并且对其他人有用;-)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM