window.addeventjs事件驱动函数集合addEvent等

时间:2021-05-25

//writtenbyDeanEdwards,2005
//withinputfromTinoZijdel,MatthiasMiller,DiegoPerini

//http://dean.edwards.name/weblog/2005/10/add-event/

functionaddEvent(element,type,handler){
if(element.addEventListener){
element.addEventListener(type,handler,false);
}else{
//assigneacheventhandlerauniqueID
if(!handler.$$guid)handler.$$guid=addEvent.guid++;
//createahashtableofeventtypesfortheelement
if(!element.events)element.events={};
//createahashtableofeventhandlersforeachelement/eventpair
varhandlers=element.events[type];
if(!handlers){
handlers=element.events[type]={};
//storetheexistingeventhandler(ifthereisone)
if(element["on"+type]){
handlers[0]=element["on"+type];
}
}
//storetheeventhandlerinthehashtable
handlers[handler.$$guid]=handler;
//assignaglobaleventhandlertodoallthework
element["on"+type]=handleEvent;
}
};
//acounterusedtocreateuniqueIDs
addEvent.guid=1;

functionremoveEvent(element,type,handler){
if(element.removeEventListener){
element.removeEventListener(type,handler,false);
}else{
//deletetheeventhandlerfromthehashtable
if(element.events&&element.events[type]){
deleteelement.events[type][handler.$$guid];
}
}
};

functionhandleEvent(event){
varreturnValue=true;
//grabtheeventobject(IEusesaglobaleventobject)
event=event||fixEvent(((this.ownerDocument||this.document||this).parentWindow||window).event);
//getareferencetothehashtableofeventhandlers
varhandlers=this.events[event.type];
//executeeacheventhandler
for(variinhandlers){
this.$$handleEvent=handlers[i];
if(this.$$handleEvent(event)===false){
returnValue=false;
}
}
returnreturnValue;
};

functionfixEvent(event){
//addW3Cstandardeventmethods
event.preventDefault=fixEvent.preventDefault;
event.stopPropagation=fixEvent.stopPropagation;
returnevent;
};
fixEvent.preventDefault=function(){
this.returnValue=false;
};
fixEvent.stopPropagation=function(){
this.cancelBubble=true;
};

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章