首页 > 代码库 > Javascript:兼容ie的事件绑定函数

Javascript:兼容ie的事件绑定函数

<script type="text/javascript">

  function addEvent(obj,ev,fn){

    if(window.addEventListener){

      obj.addEventListener(ev,fn,false);

    }else{

      obj.attachEvent("on"+ev,fn);

    }

  }

 

addEvent(document,"click",function(){

    alert(‘a‘);

});

addEvent(document,"click",function(){

    alert(‘b‘);

});

</script>