2013-06-24 55 views
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。我希望這很清楚。先謝謝你。

+0

什麼'10px屬性?這不在你的示例代碼中。問題不清楚。什麼產生新號碼?那就是你可能會做你的支票的地方。 – gibberish

+0

我已經做了必要的修改。謝謝@gibberish – Brianmuks

+0

爲什麼不圍繞'topArrtcardx'運行一個循環,然後測試'if(topArrtcardx.style.height =='10px'){do whatever}'。 – cube

回答

0

如果你不反對使用jQuery(爲什麼你會是什麼?它在許多方面比JavaScript和少得多的輸入簡單)...

我無法從你的代碼中看到(此時)什麼會觸發你可以用來做你想要的測試的事件。

但是,如果DIV發生某些事情,那麼也許您可以使用該事件進行所需的更改。您可以將更改的DIV作爲this

Here is an example看起來有點相似