js事件的建立

在jQuery上事件的处理的话就很简单了。因为jQuery都封装好了。

不过还的了解事件在不同浏览器的处理情况。

不多说,看代码。

var eventUtil = {
           addListener: function(element, type, hander){
               console.log(element);
               if(element.addEventListener){
                   element.addEventListener(type, hander, false);
               }else{
                   if(element.attachEvent){
                       element.attachEvent('on'+ type, hander);
                   }else{
                       element['on' + type] = hander;
                   }
               }
           },
           getEvent: function(event){
                   return event || window.event;
           },
           getTarget: function(event){
               return event.target || event.srcElement;
           },
           preventDefault: function(event){
               if(event.preventDefault){
                   event.preventDefault();
               }else{
                   event.returnValue = false;
               }
           },
           removeListener: function(element, type , hander){
               if(element.removeEventListener){
                   element.removeEventListener(type, hander, false);
               }else {
                   if(element.detachEvent){
                       element.detachEvent(type, hander);
                   }else{
                       element['on' + type] = null;
                   }
               }
           },
           stopPropagation: function(event){
               if(event.stopPropagation){
                   event.stopPropagation();
               }else{
                   event.cancelBubble = true;
               }
           }
       };

你可能感兴趣的:(js)