我知道在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。
当前回答
ID应该是唯一的,所以您应该在一个页面上只使用特定ID一次。类可以重复使用。
查看HTML id属性(W3Schools)了解更多详细信息。
其他回答
这很有趣,但据我所知,答案肯定是否定的。我不明白为什么您需要一个嵌套ID,因为您通常会与具有相同嵌套ID的另一个元素交叉。如果你不做,那就没有意义,如果你做了,那也没什么意义。
任何分配给div元素的ID都是唯一的。 但是,你可以在一个div元素下分配多个id,而不是“分配给”一个div元素。 在这种情况下,您必须将这些id表示为<span></span> IDs。
例如,您希望同一HTML页面中的两个链接指向页面中的相同div元素。
这两个不同的链接
<p><a href="#exponentialEquationsCalculator">Exponential Equations</a></p>
<p><a href="#logarithmicExpressionsCalculator"><Logarithmic Expressions</a></p>
指向页面的同一部分
<!-- Exponential / Logarithmic Equations Calculator -->
<div class="w3-container w3-card white w3-margin-bottom">
<span id="exponentialEquationsCalculator"></span>
<span id="logarithmicEquationsCalculator"></span>
</div>
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 :(
每个元素只能有一个ID,但确实可以有多个类。但不要有多个类属性;将多个类值放入一个属性中。
<div id="foo" class="bar baz bax">
完全合法。
ID应该是唯一的,所以您应该在一个页面上只使用特定ID一次。类可以重复使用。
查看HTML id属性(W3Schools)了解更多详细信息。