HTML5 自定义属性 - 为什么要使用它们?
我似乎不明白为什么我应该对 HTML5 允许自定义属性感到满意? 我为什么要使用它们?
I can't seem to understand why I should be happy with HTML5 allowing custom attributes? Why would I use them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设您引用的是 HTML5
[data-*]
属性。优点是您可以轻松地将一些脚本数据(仍然是语义的,但不用于显示)与您的元素关联起来,而无需在各处插入内联 javascript,并且它将是有效的 HTML5。要在 HTML4 中执行相同的操作,需要指定自定义命名空间,并添加一些命名空间属性。
假设您有一个待售商品列表,您可能希望存储数字价格而不尝试解析字符串:
如果您允许用户标记要购买的多个不同商品,您可以轻松提取数值显示运行总计。
或者,您可以将数字放入具有特定类的范围中,在正确的项目上找到正确的范围,然后以这种方式提取值,但是
[data-*]
属性会减少数量做同样的事情所需的标记/脚本。如果您不想使用它,则不需要。将数据与元素关联的方法有很多种,这只是一种新方法。
此外,新的
数据集
JavaScript API 提供了一种以声明方式访问存储在[data-*]
属性中的值的一致方法。对于 jQuery 用户,
.data()
和.attr()
可用于访问[data-*]
属性,以及我已经写了一份详细的答案,说明您何时想使用其中之一。I assume you're referencing the HTML5
[data-*]
attributes.The advantage is that you can easily associate some scripting data (still semantic, but not for display) with your elements without having to insert inline javascript all over the place, and it will be valid HTML5. To do the same thing in HTML4 would require specifying a custom namespace, and add some namespaced attributes.
Say you've got a list of items for sale, you may want to store the numeric price without trying to parse a string:
If you allow your user to mark a number of different items to buy, you can easily pull out the numeric value to display a running total.
Alternatively, you could have put the numbers in a span with a specific class, find the right span on the right item, and pull out the value that way, but
[data-*]
attributes reduce the amount of markup/script necessary to do the same thing.If you don't want to use it, you don't need to. There are many ways of associating data with elements, this is just a new one.
Additionally, the new
dataset
JavaScript API provides a consistent means of declaratively accessing the values stored in[data-*]
attributes.For jQuery users,
.data()
and.attr()
can be used to access[data-*]
attributes, and I have written up a detailed answer on when you would want to use one over the other.自定义属性已经被广泛使用,例如这里是来自dojoToolkit的示例< /a>():
现在可以重写,以便使用
data-dojoType
等属性来验证标记。它们还允许您将应用程序特定的数据存储在标签中,而不是在类属性中进行修改。对 HTML5 Doctor 上的 data-* 属性有一个很好的介绍。
Custom attributes are already widely used, for example here's an example from dojoToolkit():
This could now be re-written so that the markup validates using attributes like
data-dojoType
. They also allow you to store application specific data in your tags rather than hacking around in the class attribute.There's a good introduction to data-* attributes on HTML5 Doctor.
使用
data-
自定义属性可以为您的 html5 页面提供未来保障,遵守该规范的未来浏览器不会使用data-[attribute]
因此不与您的自定义属性冲突。Using the
data-
custom attribute is future proofing your html5 page, no future browsers adhering to the spec will usedata-[attribute]
therefore will not clash with your custom attribute.我发现了 data- 属性的另一种用途:
您可以使用新的 HTML5 自定义
data-
属性作为工具提示:纯 CSS3 和 HTML5 的语义工具提示。I found another use for the data- attribute:
You could use the new HTML5 custom
data-
attribute for tooltips: Semantic Tooltips With Pure CSS3 and HTML5.