js事件处理

var EventUtil = {//建新对象
         addHandler:function(element,type,func){//添加事件
            if(element.addEventListener){
                element.addEventListener(type,func,false);
            }else if(element.attachEvent){
                element.attachEvent("on"+ type,func);
            }else{
                element["on"+ type] = func;
            }    
         },
         getEvent:function(event){//获取Event对象
             return event?event:window.event;        
         },
         getTarget:function(event){//获得事件目标
             return event.target || event.srcElement;
         },
         preventDefault:function(event){//阻止事件的默认行为
            if(event.preventDefault){
                return event.preventDefault();//非IE
            }else{
                event.returnValue = false;//IE
            }
         },
         removeHandler:function(element,type,func){//取消事件
            if(element.removeEventListener){
element.removeEventListener(type,func,false);
            }else if(element.detachEvent){
                element.detachEvent("on"+ type,func);
            }else{
                element["on"+ type] = null;
            }
         },
         stopPropagation:function(event){//取消事件的冒泡
             if(event.stopPropagation){
                event.stopPropagation();//非IE
            }else{
                event.cancelBubble = true;//IE
            }
         }
    };
posted @ 2012-01-31 11:08  awp110  阅读(193)  评论(0编辑  收藏  举报