在多个HTML标记中使用相同的ID? [英] Using same ID for in multiple HTML tags?

查看:105
本文介绍了在多个HTML标记中使用相同的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

几个具有相同ID的元素响应一个CSS ID选择器


下面是我测试的示例代码,我很困惑。每个人都说我们可以使用或者我们应该只使用一次每个id,但我有睾丸使用它多次,但它给我正确的输出。





代码:

 < 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>

< address id =exampleID1>

Written by 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屋!

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