对多个HTML标签使用相同的ID? [英] Using same ID for multiple HTML tags?
问题描述
下面是我测试的示例代码,我感到困惑。每个人都说我们可以使用,或者我们应该每次只使用一次,但是我有睾丸多次使用它,但是它给了我正确的输出。
什么我应该怎么做?
代码:
< html>
< head>
< style>
#exampleID1 {background-color:blue; }
#exampleID2 {text-transform:uppercase; }
< / style>
< / head>
< body>
< p id =exampleID1>此段落的ID名称为exampleID1,并且具有蓝色CSS定义的背景。< / p>
< p id =exampleID2>此段落的ID名称为exampleID2,并将其文字转换为大写字母。< / p>
<地址ID =exampleID1>
由W3Schools.com撰写< br />
< a href =mailto:us@example.org>向我们发送电子邮件< / a>< br />
地址:Box 564,Disneyland< br />
电话:+12 34 56 78
< / address>
< blockquote id =exampleID1>
这里是一个长引文,这里是长引文,这里是长引文,这里是长引文,这里是长引文,这里是长引文,这里是长引文,这里是长引文,这里是长引文。
< / blockquote>
< / body>
< / html>
请参阅上面的代码并回答我们为什么不应该在页面中使用id选择器两次而其工作正常。
一个id在页面中必须是唯一的。
为什么我们不应该在页面工作时在页面中使用id选择器两次很好。
您发生错误并取决于每个浏览器都会查看页面以补偿它。你不能确定他们都会。
Possible Duplicate:
Several elements with the same ID responding to one CSS ID selector
Below is the example code that I was testing and I got confused. Every one says that we can use or we should use only one time per id, but I have testes using using it multiple times but its giving me the correct output.
What should I do?
It's kinda working same like class for me in this example
code:
<html>
<head>
<style>
#exampleID1 { background-color: blue; }
#exampleID2 { text-transform: uppercase; }
</style>
</head>
<body>
<p id="exampleID1">This paragraph has an ID name of "exampleID1" and has a blue CSS defined background.</p>
<p id="exampleID2">This paragraph has an ID name of "exampleID2" and has had its text transformed to uppercase letters.</p>
<address id="exampleID1">
Written by W3Schools.com<br />
<a href="mailto:us@example.org">Email us</a><br />
Address: Box 564, Disneyland<br />
Phone: +12 34 56 78
</address>
<blockquote id="exampleID1">
Here is a long quotation here is a long quotation here is a long quotation here is a long quotation here is a long quotation here is a long quotation here is a long quotation here is a long quotation here is a long quotation.
</blockquote>
</body>
</html>
Please see the above code and answer me that why we should not use id selector two times in a page while its working fine.
An id must be unique in a page. Use a class if you want to describe a group of elements.
why we should not use id selector two times in a page while its working fine.
You are making an error and depending on every browser that will ever view the page to compensate for it. You can't be sure they all will.
这篇关于对多个HTML标签使用相同的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!