欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js 事件兼容

程序员文章站 2022-04-27 13:14:49
...
(function(window) {
window.EventUtil ={
addEventHandler:function(element,type,handler){
if(element.addEventListener)
element.addEventListener(type,handler,false);
else if(element.attachHaattachEventdler)
element.attachEvent('on' + type,handler)
else
element['on' + type] = null;
},
removeEventHandler:function(element,type,handler){
if(element.removeEventListener)
element.removeEventListener(type,handler,false);
else if(element.detachEvent)
element.detachEvent('on' + type,handler)
else
element['on' + type] = null;
},
formatEvent:function(oEvent){
var isIE=/msie/i.test(navigator.userAgent),
isWin=/win/i.test(navigator.userAgent);
if(isIE && isWin){
oEvent.charCode=(oEvent.type == "keypress")?oEvent.keyCode:0;
oEvent.eventPhase =2;
oEvent.isChar=(oEvent.charCode>0);
oEvent.pageX=oEvent.clientX+document.body.scrollLeft;
oEvent.pageY=oEvent.clientY+document.body.scrollTop;
oEvent.preventDefault=function(){
this.returnvalue=false;
}
if(oEvent.type == "mouseout"){
oEvent.relateTarget=oEvent.toElement;
}else if(oEvent.type=="mouseover"){
oEvent.relatedTarget=oEvent.fromElement;
}
oEvent.stopPropagation=function(){
this.cancelBubble=true;
}
oEvent.target=oEvent.srcElement;
oEvent.time=(new Date()).getTime();
}
return oEvent;
},
getEvent: function(event){
if(event)
return EventUtil.getEvent.caller.arguments[0];
else
return this.formatEvent(window.event);
},
getTarget:function(event){
return event.target||event.srcElement;
},
preventDefault:function(event){
if(event.preventDefault)
event.preventDefault();
else
event.returnValue = false;
},
stopPropagation:function(event){
if(event.stopPropagation)
event.stopPropagation();
else
event.cancelBubble = true;
}
};
})(window);
相关标签: event