事件绑定

绑定多个事件

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            #box1{
                width:100px;
                height:100px;
                background: red;
            }
            #box2{
                width:200px;
                height:200px;
                background: blue;
            }
        </style>
        <script>
                    function addEvent(obj,ev,fn){
                            if(obj.attachEvent){obj.attachEvent('on'+ev,fn);
                            }else{obj.addEventListener(ev,fn,false)}
                        }
                 window.onload=function(){
                          var oBox1=document.getElementById("box1");
                          var oBox2=document.getElementById("box2");
                          addEvent(oBox1,'click',function(){
                             oBox2.style.background='green';
                          });
                          addEvent(oBox1,'mouseover',function(){
                             this.style.background='purple';
                          });
                          addEvent(oBox1,'mouseout',function(){
                             this.style.background='red';
                          });
            }
        </script>
    </head>
    <body>
        <div id="box1"></div>
        <div id="box2"></div>
    </body>
</html>

 

posted @ 2016-07-25 15:41  河南小样  阅读(98)  评论(0编辑  收藏  举报