我知道在HTML/XHTML页面中id必须是唯一的。
对于一个给定的元素,我可以给它分配多个id吗?
<div id="nested_element_123 task_123"></div>
我意识到我有一个简单的解决方案,只需使用一个类。我只是好奇以这种方式使用id。
我知道在HTML/XHTML页面中id必须是唯一的。
对于一个给定的元素,我可以给它分配多个id吗?
<div id="nested_element_123 task_123"></div>
我意识到我有一个简单的解决方案,只需使用一个类。我只是好奇以这种方式使用id。
当前回答
I don´t think you can have two Id´s but it should be possible. Using the same id twice is a different case... like two people using the same passport. However one person could have multiple passports... Came looking for this since I have a situation where a single employee can have several functions. Say "sysadm" and "team coordinator" having the id="sysadm teamcoordinator" would let me reference them from other pages so that employees.html#sysadm and employees.html#teamcoordinator would lead to the same place... One day somebody else might take over the team coordinator function while the sysadm remains the sysadm... then I only have to change the ids on the employees.html page ... but like I said - it doesn´t work :(
其他回答
Nay.
从3.2.3.1的id属性:
不能包含空格。
id="a b" <——查找该VaLuE中的空格字符。
也就是说,可以对多个id进行样式化。但如果您遵循规范,答案是否定的。
我的理解一直是:
id是一次性使用的,只应用于一个元素… 每个元素都作为唯一标识符归属于(仅)一个元素。 类可以使用不止一次… 因此,它们可以应用于多个元素,类似但不同的是,每个元素可以有多个类(即多个类)。
I don´t think you can have two Id´s but it should be possible. Using the same id twice is a different case... like two people using the same passport. However one person could have multiple passports... Came looking for this since I have a situation where a single employee can have several functions. Say "sysadm" and "team coordinator" having the id="sysadm teamcoordinator" would let me reference them from other pages so that employees.html#sysadm and employees.html#teamcoordinator would lead to the same place... One day somebody else might take over the team coordinator function while the sysadm remains the sysadm... then I only have to change the ids on the employees.html page ... but like I said - it doesn´t work :(
不,如果你想沿着那条路径走,你应该使用嵌套的div。此外,即使您可以,想象一下当您运行document.getElementByID()时它会引起的混乱。如果有多个ID,它会抓取哪个ID ?
在稍微相关的主题中,您可以向DIV中添加多个类。
http://meyerweb.com/eric/articles/webrev/199802a.html
No.
话虽如此,没有什么能阻止你这么做。但是不同的浏览器会产生不一致的行为。不要这样做。每个元素一个ID。
如果需要对一个元素进行多个赋值,请使用classes(用空格分隔)。