js切换实现背景颜色

<script type="text/javascript">
        obj=document.getElementsByTagName('h1');

        for(i=0;i<obj.length;i++){

              obj[i].setAttribute('num',0);

              obj[i].onclick=function(){

                  num=parseInt(this.getAttribute('num'));
                  if(num%2==0){
                       this.style.background='#ccc';
                  }else{
                      this.style.background='#fff';
                  } 
                  this.setAttribute('num',num+1);
            }
            
        }
</script>

 

posted @ 2016-06-06 22:19  骏码信息  阅读(1324)  评论(0编辑  收藏  举报