原生事件绑定及常用工具函数

常用原生方法封装


var util = {
      addHandler:function(ele,type,handler){
        if(ele.attachEvent){
          //IE
          ele.attachEvent('on'+type,handler)           
        }else{
          ele.addEventListener(type,handler,false)
        }
      },
      removeHandler:function(ele,type,cb){
        if(ele.detachEvent){
          ele.detachEvent('on'+type,cb)
        }else{
          ele.removeEventListener(type,cb,false);
        }
        ele['on'+type] = null;//防止内存泄漏
      },
      getEvent:function (event){
        return event?event:window.event;
      },
      getTarget:function(event){
        return event.target || event.srcElement;
      },
      preventDefault:function (event){
        if(event.preventDefault){
          event.preventDefault();
        }else{
          event.returnVauel=false;
        }
      },
      stopPropagation:function (event){
        if (event.stopPropagation){
          event.stopPropagation(); 
        }else{
          event.cancleBubble=true;
        }
      }
    }
确认 取消
1条评论