首页 > 怎么样让它点击两次后还原成原来的颜色?

怎么样让它点击两次后还原成原来的颜色?

现在的效果是三个红色的小div点击哪个就哪个就变成黄色 同时其它的div都变成红色 现在想完善一下 在上面这个基础上 点击哪个div两次 还让它还原成原来的颜色 这个效果怎么弄呢?

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
    #head{width: 400px;height: 500px;background-color: blue;margin: 0 auto;}
    #div1,#div2,#div3{width: 100px;height: 80px;background-color: red;}
    </style>
    <script type="text/javascript">
        window.onload=function(){
            var oDiv1=document.getElementById('head');
            var oDiv=oDiv1.getElementsByTagName('div')
            for(i=0;i<oDiv.length;i++){
               oDiv[i].onclick=function(){
                    for(j=0;j<oDiv.length;j++){
                      oDiv[j].style.backgroundColor="red"
                      this.style.backgroundColor="yellow"
                    }
                }
                
            }
            
        }
    </script>
</head>
<body>
<div id="head">
    <div id="div1"></div><br />
    <div id="div2"></div><br />
    <div id="div3"></div>
</div>
</body>
</html>

测试完毕,来得晚了点。代码的关键解析在最下面。


<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
    #head{width: 400px;height: 500px;background-color: blue;margin: 0 auto;}
    #div1,#div2,#div3{width: 100px;height: 80px;background-color: red;}
    </style>
    <script type="text/javascript">
        window.onload = function(){
            var oDiv1 = document.getElementById('head');
            var oDiv = oDiv1.getElementsByTagName('div')
            for(i = 0; i < oDiv.length; i++){
               oDiv[i].onclick=function(){
                    for(j=0; j<oDiv.length; j++){
                        if (this.getAttribute("style") == "background-color: yellow;") {
                            this.style.backgroundColor = "red";
                        } else {
                            oDiv[j].style.backgroundColor = "red";
                            this.style.backgroundColor = "yellow";
                        }
                    }
                }
            }
        }
    </script>
</head>
<body>
<div id="head">
    <div id="div1"></div><br />
    <div id="div2"></div><br />
    <div id="div3"></div>
</div>
</body>
</html>

重点在于这边,当自身被点中后,如果style是黄色,则改变自身,否则就按旧有逻辑走:

for(j=0; j<oDiv.length; j++){
    if (this.getAttribute("style") == "background-color: yellow;") {
        this.style.backgroundColor = "red";
    } else {
        oDiv[j].style.backgroundColor = "red";
        this.style.backgroundColor = "yellow";
    }
}

以及,代码格式希望注意一下,别让人读起来费劲,反正最后可以压缩。


想的不算仔细,给你个粗略的方案吧:

window.onload=function(){
    var oDiv1=document.getElementById('head');
    var oDiv=oDiv1.getElementsByTagName('div');
    var timeId;
    for(i=0;i<oDiv.length;i++){
       oDiv[i].addEventListener('click', function(){
           var _this = this;
           clearTimeout(timeId);
           timeId = setTimeout(function() {
               for(j=0;j<oDiv.length;j++){
                 oDiv[j].style.backgroundColor="red"
                 _this.style.backgroundColor="yellow"
               }
           }, 200);
       }, false);

        oDiv[i].addEventListener('dblclick', function(){
            clearTimeout(timeId);
            this.style.backgroundColor="red";
        }, false);

    }
}

后面你可以自己尝试不断优化,思路就这样,哪里有bug的自己调调吧,我这也是手打的,没试过


我在里面做一个if判断这样试了一下还是不行

oDiv[i].onclick=function(){
                    for(j=0;j<oDiv.length;j++){
                      oDiv[j].style.backgroundColor="red"
                      if(this.style.backgroundColor=="red"){
                          this.style.backgroundColor="yellow"
                      }else{
                          this.style.backgroundColor="red"
                      }
                    }
                    
                }

两点吧,第一,点击元素颜色发生改变,写一个on-off开关来判断,改变换成哪个颜色。
第二,就是点击某一div后,其他div颜色发生变化,这里可以有两个做法,1、当某一div颜色变黄(依据on-off开关判定)时,将其余所有div颜色赋值为红色。2、给每个div一个索引值,当某一div变黄时,记录下该索引值,当其他div要变换颜色时,将记录下索引值对应的div的颜色变红。后一种方法的效率会高一些。
手机端回答问题,不方便写代码了。

【热门文章】
【热门文章】