問題描述
可能重復(fù):
多個(gè)具有相同ID的元素響應(yīng)一個(gè) CSS ID 選擇器
下面是我正在測(cè)試的示例代碼,我很困惑.每個(gè)人都說我們可以使用或者我們應(yīng)該每個(gè) id 只使用一次,但我有測(cè)試使用它多次但它給了我正確的輸出.
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.
我該怎么辦?
在這個(gè)例子中,它有點(diǎn)像我的類
It's kinda working same like class for me in this example
代碼:
<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>
請(qǐng)查看上面的代碼并回答我為什么我們不應(yīng)該在一個(gè)頁面中使用兩次 id 選擇器而它工作正常.
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.
推薦答案
一個(gè) id 在一個(gè)頁面中必須是唯一的.如果要描述一組元素,請(qǐng)使用類.
An id must be unique in a page. Use a class if you want to describe a group of elements.
為什么我們不應(yīng)該在一個(gè)頁面中使用 id 選擇器兩次而它工作正常.
why we should not use id selector two times in a page while its working fine.
您犯了一個(gè)錯(cuò)誤,這取決于每個(gè)瀏覽該頁面的瀏覽器來彌補(bǔ)它.你不能確定他們都會(huì).
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.
這篇關(guān)于對(duì)多個(gè) HTML 標(biāo)簽使用相同的 ID?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!