<!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 ></div>
<div ></div>
<div ></div>
<div ></div>
</body>
</html>

 

相关文章: