一个 HTML 元素可以有多个唯一的 ID 属性吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16936132/
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
Can a HTML element have multiple unique ID attributes?
提问by Abhishek Madhani
Needed to know if an HTML element can have multiple attributes of ID's, for instance :
需要知道一个 HTML 元素是否可以具有多个 ID 属性,例如:
<input type="text" id="identifier1" id="selector1" />
As I needed to clarifythis statement mentioned about selectors at W3 website.
因为我需要澄清在W3 网站上提到的有关选择器的声明。
If an element has multiple ID attributes, all of them must be treated as IDs for that element for the purposes of the ID selector. Such a situation could be reached using mixtures of xml:id, DOM3 Core, XML DTDs, and namespace-specific knowledge.
如果一个元素有多个 ID 属性,则为了 ID 选择器的目的,所有这些属性都必须被视为该元素的 ID。这种情况可以通过混合使用 xml:id、DOM3 Core、XML DTD 和特定于名称空间的知识来实现。
The Possible duplicates which people are referring, states question for this syntax
人们所指的可能的重复项说明了对此语法的问题
<input type="text" id="identifier1 selector1" />
which is different than syntax that I am asking.
这与我要问的语法不同。
回答by mattytommo
Needed to know if an HTML element can have multiple attributes of ID's
需要知道一个 HTML 元素是否可以有多个 ID 属性
Short answer? No because the browser will only render the first one.
简短的回答?不,因为浏览器只会呈现第一个。
See this Fiddle, I can only target it in CSS using the first id
that appears in the DOM. Try changing that CSS selector to use the second id
, it won't work.
看到这个 Fiddle,我只能使用id
出现在 DOM 中的第一个在 CSS 中定位它。尝试更改该 CSS 选择器以使用第二个id
,它不起作用。
That's because it seems like the second ID is being disregarded by the browser, as this is the output HTML:
那是因为浏览器似乎忽略了第二个 ID,因为这是输出 HTML:
<input type="text" id="identifier1">
If you really need additional identifiers on an element, you should think about using either multiple class names or data attributes to correspond to additional data.
如果你真的需要一个元素的额外标识符,你应该考虑使用多个类名或数据属性来对应额外的数据。
回答by Quentin
Needed to know if an HTML element can have multiple attributes of ID's
需要知道一个 HTML 元素是否可以有多个 ID 属性
No. No element in HTML may have more then one instance of a given attribute.
不可以。HTML 中的任何元素都不能具有多个给定属性的实例。
As I needed to clarify this statement
因为我需要澄清这个声明
Note the last sentence in that statement.
请注意该声明中的最后一句话。
Also note that the CSS idea of an "ID attribute" is not "An attribute with the name id
". Also quoting from that document:
另请注意,“ID 属性”的 CSS 思想不是“具有名称的属性id
”。还引用了该文件:
Document languages may contain attributes that are declared to be of type ID
文档语言可能包含声明为 ID 类型的属性
Only the id
attribute is an ID type in HTML.
只有id
属性是 HTML 中的 ID 类型。
回答by Tapan Chandra
No, even if you specify multiple ids, the first encountered id attribute is used.
不,即使您指定多个 id,也会使用第一个遇到的 id 属性。
Possible duplicates :
可能的重复:
回答by Jukka K. Korpela
No, because an attribute must not be repeated in tag. This is a general rule in HTML, not limited to the id
attribute. For nominally SGML-based versions of HTML and for XHTML, this follows from general SGML and XML rules. For HTML serialized HTML5, see HTML5 CR, 8.1.2.3 Attributes.
不可以,因为属性不能在标签中重复。这是 HTML 中的通用规则,不限于id
属性。对于名义上基于 SGML 的 HTML 版本和 XHTML,这遵循一般的 SGML 和 XML 规则。对于 HTML 序列化的 HTML5,请参阅HTML5 CR, 8.1.2.3 Attributes。
It's difficult to see why you would use duplicate id
attributes, so I can't suggest a workaround. In general, for any normal use of the id
attribute, one attribute per element is sufficient.
很难理解为什么要使用重复的id
属性,因此我无法提出解决方法。一般来说,对于id
属性的任何正常使用,每个元素一个属性就足够了。
回答by Abuzer Firdousi
No IDcan not be same for html elements, but the Classes are to use for multiple elements, and one element may have multiple classes
html元素没有ID不能相同,但是Classes是为多个元素使用的,一个元素可以有多个classes
回答by Rubyist
No. Element IDs should be unique within the entire document.
不可以。元素 ID 在整个文档中应该是唯一的。