javascript 对旧浏览器使用 HTML5 的“data-*”属性有什么问题吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3957867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:49:23  来源:igfitidea点击:

Is there any problem with using HTML5's "data-*" attributes for older browsers?

javascriptjqueryhtmlcross-browsercustom-data-attribute

提问by Rory

I want to associate some custom data with some HTML nodes. I was going to use the new HTML5 style 'data-*' attributes. e.g.: <tr class="foo" data-typeid="7">…, and then I was going to select this HTML node and show/hide it etc. by reading the value with $(node).attr("data-typeid").

我想将一些自定义数据与一些 HTML 节点相关联。我打算使用新的 HTML5 样式 'data-*' 属性。例如:<tr class="foo" data-typeid="7">…,然后我要选择这个 HTML 节点并通过读取值来显示/隐藏它等$(node).attr("data-typeid")

However this web page needs to work with older browsers aswell. I'm not using the data-* attribute as a special attribute, but I'd like to know if older browsers will ignore, wipe, or make inaccessible this attribute since it's not valid HTML4.

但是,此网页也需要与较旧的浏览器一起使用。我没有使用 data-* 属性作为特殊属性,但我想知道旧浏览器是否会忽略、擦除或使该属性无法访问,因为它不是有效的 HTML4。

回答by Nick Craver

There isn't really, they're not 100% correct/valid usage in HTML4 of course....but they don't cause problems either, so they're still a great way to solve the "I need an attribute for this" problem.

真的没有,当然,它们在 HTML4 中不是 100% 正确/有效的使用......但它们也不会引起问题,所以它们仍然是解决“我需要一个属性的好方法这个问题。

If it helps, I've used these while supporting IE6 and have had zero issues thus far, and I can't recall a single SO question reporting any either.

如果有帮助,我已经在支持 IE6 的同时使用了这些,并且到目前为止已经有零问题,而且我不记得有一个 SO 问题报告任何问题。

回答by Jacob Rask

Internet Explorer and Microsoft has added several custom attributes that are not valid HTML4. Browsers don't check the element attributes against a specification, you can name an attribute roryscoolinfo="hello"if you like (though you shouldn't).

Internet Explorer 和 Microsoft 已添加了几个不是有效 HTML4 的自定义属性。浏览器不会根据规范检查元素属性,您可以根据需要命名属性roryscoolinfo="hello"(尽管您不应该这样做)。

The Dojo Toolkit adds its custom dojo*attributes.

Dojo Toolkit 添加了它的自定义dojo*属性。

It's fine to use data-today, with a HTML5 doctype.

data-今天可以使用HTML5 文档类型。