<!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 = "gray";
divs[j].style.color="gray";
divs[j].style.fontWeight="normal";
}
this.style.backgroundColor = "white";
this.style.color="#00BCDE";
this.style.fontWeight="bold";
};
}
};
</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>
javascript动态实现div变色
最新推荐文章于 2022-11-01 23:35:54 发布