0

我有三个 div,我想在一个恰好位于容器中其他 div 之上的 div 上执行一个命令,而不引用它的名称或 id。我正在随机化 div 的位置,我基本上想要高度的 div等于在10px生成特定数字时将其颜色属性更改为红色,而其他 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。我希望这很清楚。先感谢您。

4

1 回答 1

0

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

我无法从您的代码(此时)中看到什么会触发一个事件,您可以使用该事件来进行您想要的测试。

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

这是一个看起来有点相似的例子

于 2013-06-24T00:09:45.950 回答