简体   繁体   English

HTML“数据属性”与简单的“自定义属性”

[英]HTML "data-attribute" vs simple "custom attribute"

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.我通常会看到 html data-attribute (s) 将特定值/参数添加到 html 元素,例如 Bootstrap,它使用它们将按钮“链接”到要打开的模态对话框等。

<button type="button" class="close" data-dismiss="modal" aria-label="Close">

Now, I see that an almost famous CSS framework, Kube , in its new version extensively uses simple custom attribute such as in following:现在,我看到一个几乎著名的 CSS 框架Kube在其新版本中广泛使用简单的自定义属性,如下所示:

<column cols="4">4</column> 

<span class="label label-black" outline bold>Black</span>

Other in-action examples are visibile for example, here .其他在行动中的例子是可见的,例如,这里

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.我不知道可以使用简单的自定义属性,所以我试图搜索一些关于此的来源,我只发现了这个旧的类似问题,其中几乎只有(可能的)兼容性问题。

I'm surprised that a CSS framework like Kube could use a similar solution if browser support could be so "fragile"....我很惊讶,如果浏览器支持如此“脆弱”,像 Kube 这样的 CSS 框架可以使用类似的解决方案......

So my question are:所以我的问题是:

  1. How good (=cross-compatible) is Kube's approach? Kube 的方法有多好(=交叉兼容)
  2. Can I safely replace my data-attribute with simple custom ones if for example I have to pass only true/false values?例如,如果我只需要传递真/假值,我可以用简单的自定义data-attribute安全地替换我的data-attribute吗?

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>最后一个问题最好用一个例子来描述,所以用<span class="foo" data-boo='true'>Black</span>替换<span class="foo" data-boo='true'>Black</span> <span class="foo" boo>Black</span>

Use data attributes.使用数据属性。 They are:他们是:

  • standard标准
  • won't make a validator report them as errors (it's hard to spot unintentional errors in a validation report when they are surrounded with a pile of errors that you've made intentionally)不会让验证器将它们报告为错误(当它们被一堆你故意犯的错误包围时,很难在验证报告中发现无意的错误)
  • won't conflict with attributes that might be added to the standard in the future不会与将来可能添加到标准中的属性发生冲突

… 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. ……如果您只是想检查它们是否与 CSS 属性选择器一起存在,则不必为它们提供值……但如果您想这样做,那么您可能应该使用其他类来代替。

beside the fact screen readers ignore data-* attributes, the only other real-world, tangible advantage is slightly neater syntax.除了屏幕阅读器忽略data-*属性这一事实之外,唯一的另一个现实世界的、有形的优势是稍微简洁的语法。 Eg例如

el.dataset.something = 'hello'

instead of代替

el.setAttribute('something', 'hello')

Beside that, I really don't see any difference.除此之外,我真的看不出有什么区别。 Everything you can do with data-* attributes you can do with regular attributes.您可以使用data-*属性执行的所有操作都可以使用常规属性执行。

If you are using custom elements (eg my-something ) then you are free to create any attribute you like (whether regular or data-* ) because you own and control that unique element.如果您使用自定义元素(例如my-something ),那么您可以自由创建您喜欢的任何属性(无论是常规属性还是data-* ),因为您拥有并控制该唯一元素。 You can add arbitrary attributes to standard HTML elements too but you run the miniscule risk that third-party code in your project (eg a web framework) might also set the same attribute and clobber your value.您也可以向标准 HTML 元素添加任意属性,但您面临的风险很小,项目中的第三方代码(例如 Web 框架)也可能设置相同的属性并破坏您的值。

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

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