HTML元素可以有多个唯一的ID属性吗? [英] Can a HTML element have multiple unique ID attributes?

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

问题描述

需要知道HTML元素是否可以具有ID的多个属性,例如:

 < input type =textid =identifier1id =selector1/> 

因为我需要阐明这个陈述在 W3网站


如果一个元素具有多个ID属性,则为ID选择器的目的,所有元素都必须作为该元素的ID处理
。这种
的情况可以通过混合使用xml:id,DOM3 Core,XML
DTD和特定于名称空间的知识来实现​​。


人们所指的可能重复的部分对此语法表示怀疑

 <输入type =textid =identifier1 selector1/> 

这与我要求的语法不同。


简短的回答? 否,因为浏览器只显示第一个。



请参阅这个小提琴,我只能使用DOM中出现的第一个 id 来定位它。尝试更改该CSS选择器以使用第二个 id ,它将无法工作。



这是因为它看起来像第二个ID被浏览器忽略,因为这是HTML输出:

 < input type =textid = 标记位 > 

如果您确实需要元素上的其他标识符,您应该考虑使用多个类名称或数据属性以对应于其他数据。

Needed to know if an HTML element can have multiple attributes of ID's, for instance :

<input type="text" id="identifier1" id="selector1" />

As I needed to clarify this statement mentioned about selectors at W3 website.

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.

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.

解决方案

Needed to know if an HTML element can have multiple attributes of ID's

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.

That's because it seems like the second ID is being disregarded by the browser, as this is the output 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.

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

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