我为什么要prePEND与&QUOT我的自定义属性;数据 - "? [英] Why should I prepend my custom attributes with "data-"?

查看:153
本文介绍了我为什么要prePEND与&QUOT我的自定义属性;数据 - "?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,任何自定义的数据属性,我使用应该开始数据 -

So any custom data attribute that I use should start with "data-":

<li class="user" data-name="John Resig" data-city="Boston"
     data-lang="js" data-food="Bacon">
  <b>John says:</b> <span>Hello, how are you?</span>
</li>

请问什么坏事发生,如果我只是坐视不理?即:

Will anything bad happen if I just ignore this? I.e.:

<li class="user" name="John Resig" city="Boston"
     lang="js" food="Bacon">
  <b>John says:</b> <span>Hello, how are you?</span>
</li>

我猜个糟糕的事情是,我的自定义属性可以与HTML冲突具有特殊意义(如,名称),但除了这一点,有一个问题属性只是写EXAMPLE_TEXT而不是数据EXAMPLE_TEXT? (它不会验证,但谁在乎呢?)

I guess one bad thing is that my custom attributes could conflict with HTML attributes with special meanings (e.g., name), but aside from this, is there a problem with just writing "example_text" instead of "data-example_text"? (It won't validate, but who cares?)

推荐答案

有保持定制几个好处属性数据pfixed $ P $ - *。

There are several benefit for keeping custom attributes prefixed with data-*.


  1. 它保证不会有与扩展HTML在未来的版本冲突。这是一个已经遇到了在一定程度上已经与一些在HTML5中,引入的新特性,其中现有的网站正在使用的属性具有相同的名称和期待不同,不兼容,自定义行为的问题。 (例如,在输入元素是已知有对过去一些热门网站的一些冲突的要求属性)

  1. It guarantees there will not be any clashes with extensions to HTML in future editions. This is a problem that has been encountered to some degree already with some of the new attributes introduced in HTML5, where existing sites were using attributes with the same name and expecting different and incompatible, custom behaviour. (e.g. the required attribute on input elements is known to have had some clashes on some popular websites in the past)

在浏览器支持的功能,它们将提供从脚本访问这些属性更方便的DOM API。

Once browsers support the feature, they will provide a more convenient DOM API for accessing these attributes from scripts.

它们提供了一个明确的指标,其中的属性是自定义属性,而哪些是标准化的属性。这不仅让他们允许任何属性与数据 - *,同时仍然执行有用的错误其他属性检查(如捉错别字)帮助验证,这也有助于使源$ ​​C ​​$这方面C更清楚那些读它包括谁可能原作者后,在网站上工作的人。

They provide a clear indication of which attributes are custom attributes, and which ones are standardised attributes. This not only helps validators by allowing them to permit any attribute with data-* while still performing useful error checking for other attributes (e.g. to catch typos), it also helps make this aspect of the source code clearer to those reading it, including people who may work on a website after the original author.

这篇关于我为什么要prePEND与&QUOT我的自定义属性;数据 - &QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆