HTML元素可以有两次相同的属性吗? [英] Can an HTML element have the same attribute twice?

查看:115
本文介绍了HTML元素可以有两次相同的属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑编写能够产生重复属性的HTML标记的代码,如下所示:

I'm considering writing code which produces an HTML tag that could have duplicate attributes, like this:

<div data-foo="bar" class="some-class" data-foo="baz">

这是合法的HTML吗?其中一个 data-foo -values是否优先于另一个?我可以依靠半现代浏览器(IE> = 9)解析它吗?

Is this legal HTML? Does one of the data-foo-values take precendence over the other? Can I count on semi-modern browsers (IE >= 9) to parse it without choking?

或者我在这里做一些非常愚蠢的事情?

Or am I about to do something really stupid here?

推荐答案

元素中有两次相同的属性名是无效的。这方面的权威性参考文献有点复杂,因为旧的HTML版本名义上基于SGML,并且这种限制是由对SGML标准的规范性引用所隐含的。在HTML5 PR中,第8.1.2.3节属性明确指出:绝对不能有两个或更多的属性在相同的开始标签上,这些标签的名称是ASCII大小写不敏感匹配的对象。

It is not valid to have the same attribute name twice in an element. The authoritative references for this are somewhat complicated, as old HTML versions were nominally based on SGML and the restriction is implied by a normative reference to the SGML standard. In HTML5 PR, section 8.1.2.3 Attributes explicitly says: "There must never be two or more attributes on the same start tag whose names are an ASCII case-insensitive match for each other."

实际情况是后者的属性被忽略。那么,未来的浏览器可能会这样做。在DOM中,属性显示为元素节点的属性以及属性对象,所以没有自然的方法来存储两个值。

What happens in practice is that the latter attribute is ignored. Well, future browsers might do otherwise. In the DOM, attributes appear as properties of the element node as well as in the attributes object, so there would be no natural way to store two values.

这篇关于HTML元素可以有两次相同的属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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