多个元素都需要添加点击开关的例子

点击一下<li>背景图片变换,再点击一下回到原来的图片

var aLi=document.getElementsByTagName["li"];
for(var i=0;i<aLi.length;i++){
    aLi[i].onoff=true;
    aLi[i].onclick=function(){
        if(this.onoff)
            this.style.background="地址1"this.onoff=false;
        }else{
            this.style.background="地址2"this.onoff=true;
        }
    }   
}
<ul>
    <li></li>
    <li></li>
    <li></li>
</ul>

 

posted @ 2017-04-18 11:37  念念念不忘  阅读(96)  评论(0)    收藏  举报