HTML“数据属性”与简单的“自定义属性”
我通常会看到html data-attribute
(s)向html元素(例如Bootstrap)添加特定的值/参数,这些值/参数将它们用于 link
I usually saw html data-attribute
(s) to add specific values/parameters to html element like Bootstrap that uses them to "link" buttons to modal dialog to be opened, etc.
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
现在,我看到几乎是著名的CSS框架 Kube 在其新版本中广泛使用了简单的自定义属性,例如:
Now, I see that an almost famous CSS framework, Kube, in its new version extensively uses simple custom attribute such as in following:
<column cols="4">4</column>
<span class="label label-black" outline bold>Black</span>
其他实际情况示例也很明显,例如此处。
Other in-action examples are visibile for example, here.
我不知道可以使用简单的自定义属性,所以我试图搜索有关此内容的资料,但我只发现了这个古老的类似问题,几乎只注意到( possible )兼容性问题。
I did not know that was possible to use simple custom attributes so I tried to search some source about this, and I found only this old similar question in which are noted almost only (possible) compatiblity issues.
我很惊讶Kube这样的CSS框架可以使用类似的解决方案如果浏览器支持如此脆弱。...
所以我的问题是:
- 水平(=兼容) Kube的方法是吗?
- 我可以安全地更换吗?我的
数据属性
具有简单的自定义值,例如,如果我只需要传递true / false值?
- How good (=cross-compatible) is Kube's approach?
- Can I safely replace my
data-attribute
with simple custom ones if for example I have to pass only true/false values?
通过示例更好地描述了最后一个问题,因此请替换< span class = foo数据-boo ='true'> Black< / span>
与< span class = foo boo> Black< / span>
This last question is better described by an example, so replace <span class="foo" data-boo='true'>Black</span>
with <span class="foo" boo>Black</span>
使用数据属性。它们是:
Use data attributes. They are:
- 标准
- 不会使验证器阻塞
- 不会与将来可能会添加到标准中的属性冲突
…而您不会如果您只想使用CSS属性选择器检查它们是否存在,则必须给它们提供值。但是,如果您希望这样做,则应该只使用其他类。
… and you don't have to give them values if you just want to check to see if they exist with a CSS attribute selector … but if you want that then you should probably just be using additional classes instead.