根据其属性检测div的位置

时间:2013-06-24 00:00:28

标签: javascript html

我有三个div,我想在div上执行一个命令,恰好在容器中的其他div之上,而不引用它的名称或id。我随机化了div的位置,我基本上想要高度等于10px的div,在生成特定数字时将其颜色属性更改为红色,而其他div保持其默认颜色。我尝试过以下操作,但如果不使用div的id,我想不出任何方法可以做到这一点。

      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。我希望这很清楚。先感谢您。

1 个答案:

答案 0 :(得分:0)

如果你不反对使用jQuery(为什么你会这样?在很多方面它比javascript更简单,输入更少)...

我无法从您的代码(此时)看到什么会触发您可以用来进行所需测试的事件。

但是,如果DIV发生了某些事情,那么也许您可以使用该事件进行所需的更改。您可以将更改后的DIV引用为this

看起来有点相似的

Here is an example