0
我有三個div,我想執行一個div上的命令,恰好在容器中的其他div頂部,而無需引用它的名稱或id.I隨機化位置的div,我基本上想要div的高度等於10px
將其顏色屬性更改爲紅色時生成一個特定的數字,而其他div保持其默認顏色。我已經嘗試了以下,但我想不出任何方式做到這一點,而不使用div的ID。檢測div的位置基於它的attaribute
var current = 0;
current++;
var topArrtcard = document.getElementById("card-answer");
var topArrtcard1 = document.getElementById("card-answer1");
var topArrtcard2 = document.getElementById("card-answer2");
if(current === 0){
topArrtcard.style.color = "red"; // is it possible not use the id in order to make the change
topArrtcard1.style.color = " #996600";
topArrtcard2.style.color = " #996600";
}else if(current === 1)
{
topArrtcard.style.color = "#996600";
topArrtcard1.style.color = "red";
topArrtcard2.style.color = " #996600";
}else if(current === 2){
topArrtcard.style.color = "#996600";
topArrtcard1.style.color = " #996600";
topArrtcard2.style.color = "red";
}else {
topArrtcard.style.color = "#996600";
topArrtcard1.style.color = " #996600";
topArrtcard2.style.color = "#996600";
}
變量current
每次頁面加載時都會加1。我希望這很清楚。先謝謝你。
什麼'10px屬性?這不在你的示例代碼中。問題不清楚。什麼產生新號碼?那就是你可能會做你的支票的地方。 – gibberish
我已經做了必要的修改。謝謝@gibberish – Brianmuks
爲什麼不圍繞'topArrtcardx'運行一個循環,然後測試'if(topArrtcardx.style.height =='10px'){do whatever}'。 – cube