Home > Web Front-end > JS Tutorial > body text

JavaScript event listener implementation code constructed by traditional event model_javascript skills

WBOY
Release: 2016-05-16 18:26:05
Original
1210 people have browsed it

代码如下:

复制代码 代码如下:

var br={};
br.eventRouter = {};
br.eventRouter.addListen = function(el,eventType,func){
if(!el.eventObjs){
el.eventObjs = {};
}
if(!el.eventObjs[eventType]){
el.eventObjs[eventType]=[];
br.eventRouter.bindListen(el,eventType);
}
el.eventObjs[eventType].push(func);
};
br.eventRouter.removeListen = function(el,eventType,func){
if(el.eventObjs && el.eventObjs[eventType]){
for(var i=0,len=el.eventObjs[eventType].length;ivar item = el.eventObjs[eventType][i];
if(func==item){
el.eventObjs[eventType].splice(i,1);
return true;
}
}
}
return false;
};
br.eventRouter.bindListen = function(el,eventType){
if(el.eventObjs && el.eventObjs[eventType]){
el[eventType] = function(e){
for(var i=0,len=el.eventObjs[eventType].length;ivar f = el.eventObjs[eventType][i];
f.call(el,e);
}
};
}
};

代码比较简单,不做过多解释。也希望有兴趣的同学给出bug。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template