手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >window.addeventjs事件驱动函数集合addEvent等
window.addeventjs事件驱动函数集合addEvent等
摘要://writtenbyDeanEdwards,2005//withinputfromTinoZijdel,MatthiasMiller,Di...

//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;

};

【window.addeventjs事件驱动函数集合addEvent等】相关文章:

tagName的使用,留一笔

jQuery实现强制cookie过期方法汇总

深入理解JavaScript中的对象

javascript常用方法总结

Js的Array数组对象详解

accesskey 提交

javascript+HTML5自定义元素播放焦点图动画

jquery合并表格中相同文本的相邻单元格

document对象execCommand的command参数介绍

JavaScript获得url查询参数的方法

精品推荐
分类导航