<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>以最简单的代码实现需求</title>
</head>
<body>
<ul id="ul1">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<script type="text/javascript">
var oUl=document.getElementById('ul1');
var aLi=oUl.getElementsByTagName('li');
var str="";
for (var i = 0; i < aLi.length; i++) {
aLi[i].bBtn=true;
if(i%3==0){
aLi[i].color="red";
aLi[i].style.background="red";
}else if(i%3==1){
aLi[i].color="yellow";
aLi[i].style.background="yellow";
}else{
aLi[i].color="blue";
aLi[i].style.background="blue";
}
aLi[i].onmouseover=function(){
if (this.bBtn) {
this.style.background="green";
};
}
aLi[i].onmouseout=function(){
if (this.bBtn) {
this.style.background=this.color;
};
}
aLi[i].onclick=function(){
if(this.bBtn){
this.style.background="#000";
this.bBtn=false;
}else{
this.style.background=this.color;
this.bBtn=true;
}
}
};
</script>
</body>
</html>
利用开关与for实现的小例子
最新推荐文章于 2022-04-18 10:50:41 发布