任意创建偶数个button,每两个button一行,通过点击任何按钮,可以实现相邻按钮间的颜色转换...

<html> 

 <head> 

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

 <title>点击转换颜色</title> 

 <style> 

 body{background-color:151E23} 

 #divIndexTop{width:100%;height:40px; position:absolute;} 

 .btnTop{border-width:0px;width:80px;height:25px; margin:0px; padding:0px;color:#666666;} 

 .btnTopOut{border-width:0px;width:80px;height:25px;margin:0px; padding:0px;} 

 .btnTopOver{border-width:0px;width:80px;height:25px;margin:0px; padding:0px;cursor:hand;font-weight:bold; background-color:#FFFFFF;} 

 </style> 

 </head> 

 <body>   <div id="button" align="center"> </div>

 <script> 

var cnt = 8; for(var i=1;i<cnt+1;i++){ var input=document.createElement("input"); input.type="button"; input.id=i; input.value="button"+i; document.getElementById("button").appendChild(input); if(i % 2 == 0){ var br=document.createElement("br"); input.style.backgroundColor="blue"; document.getElementById("button").appendChild(br); } else {  input.style.backgroundColor="red"; }  }

 var btns=document.getElementsByTagName("input"); 

     //触发onMouseOver事件时      window.onload = function(){  var arr = document.getElementById("button").getElementsByTagName('input');  for(var i = 0;i<arr.length;i++){                 arr[i].onclick = function(){                  if(parseInt(this.id) % 2 == 0 ){               var o=document.getElementById(this.id);               var o1=document.getElementById(parseInt(this.id)-1);      if (o.style.backgroundColor != o1.style.backgroundColor)      {      var tmp = o.style.backgroundColor;      o.style.backgroundColor = o1.style.backgroundColor;      o1.style.backgroundColor = tmp;      }           }      else{              var o=document.getElementById(this.id);               var o1=document.getElementById(parseInt(this.id)+1);      if (o.style.backgroundColor != o1.style.backgroundColor)      {      var tmp = o.style.backgroundColor;      o.style.backgroundColor = o1.style.backgroundColor;      o1.style.backgroundColor = tmp;      }       }              }            }        }

     btns[0].οnclick=function () 

     { 

         this.className="btnTopOver";                    var o=document.getElementById("1");          o.style.backgroundColor="blue";  //根据你的 需要修改颜色即可

         var o=document.getElementById("2");          o.style.backgroundColor="red";         // this.style.color="#000000"; 

     }; 

     //触发onMouseOut事件时 

   //  btns[1].οnclick=function () 

    // { 

    //     this.className="btnTopOut";          //   var o=document.getElementById("1");       //   o.style.backgroundColor="red";  //根据你的 需要修改颜色即可

      //   var o=document.getElementById("2");       //   o.style.backgroundColor="blue";

   //  }; 

 </script> 

 </body> 

</html>

转载于:https://www.cnblogs.com/chllovegeyuting/archive/2013/03/02/2940421.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值