鼠标点击DIV后,DIV的背景变色(js)

<!DOCTYPE html>
<html>
<head>
<script>
    window.onload = function(){
        var divs = document.getElementsByTagName("div");
        var len = divs.length;
        for(var i=0;i<len;i++){
            divs[i].onclick = function(){
                for(var j=0;j<len;j++){
                    divs[j].style.backgroundColor = "black";
                }
                this.style.backgroundColor = "red";
            };
        }
    };
</script>
</head>
<body>
<div id="div1" style="width:100px;height:100px;background:black;margin:10px;"></div>
<div id="div2" style="width:100px;height:100px;background:black;margin:10px;"></div>
<div id="div3" style="width:100px;height:100px;background:black;margin:10px;"></div>
<div id="div4" style="width:100px;height:100px;background:black;margin:10px;"></div>
</body>
</html>

 

posted @ 2016-07-29 16:21  怪咖咖  阅读(8224)  评论(0编辑  收藏  举报