ID名稱不應該被重複使用,將其更改爲一個類名來代替,但他們仍然需要一個唯一的ID名稱爲每個爲我們的Javascript邏輯應用到他們。
HTML:
<a id="button1" class="button"></a>
<a id="button2" class="button"></a>
CSS:
.button
{
display:inline-block;
height:10px;
width:10px;
border-radius:10px;
}
的Javascript:
document.getElementById("button1").style.backgroundColor ="gray";
document.getElementById("button2").style.backgroundColor ="gray";
document.getElementById("button1").onclick = function(){
this.style.backgroundColor ="black";
document.getElementById("button2").style.backgroundColor ="gray";
};
document.getElementById("button2").onclick = function(){
this.style.backgroundColor ="black";
document.getElementById("button1").style.backgroundColor ="gray";
};
不要將相同的id分配給多個元素。這是課程的目的。 – Jecoms
一個ID在你的頁面上必須是唯一的 – gr3g
你試過了什麼?我在這裏沒有看到任何努力。 SO不是代碼寫入服務。此外,這種類型的功能已經覆蓋了數百萬次。 – hungerstar