我知道在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。
当前回答
不。每个DOM元素(如果它有id)都有一个唯一的id。你可以用以下方法来近似:
<div id='enclosing_id_123'><span id='enclosed_id_123'></span></div>
然后使用导航来得到你真正想要的东西。
如果你只是想应用样式,类名会更好。
其他回答
不,单个标记不能有多个id,但我见过带有name属性和id属性的标记,一些应用程序对它们的处理是相同的。
ID应该是唯一的,所以您应该在一个页面上只使用特定ID一次。类可以重复使用。
查看HTML id属性(W3Schools)了解更多详细信息。
No.
话虽如此,没有什么能阻止你这么做。但是不同的浏览器会产生不一致的行为。不要这样做。每个元素一个ID。
如果需要对一个元素进行多个赋值,请使用classes(用空格分隔)。
不。虽然W3C对HTML 4的定义似乎没有明确地涵盖您的问题,但name和id属性的定义表明标识符中没有空格:
ID和NAME令牌必须以字母([a- za -z])开头,后面可以是任意数量的字母、数字([0-9])、连字符("-")、下划线("_")、冒号(":")和句号(".")。
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 :(