javascript 传统事件模型构造的事件监听器实现代码
时间:2021-11-17 13:17:51|栏目:JavaScript代码|点击: 次
代码如下:
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;i<len;i++){
var 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;i<len;i++){
var f = el.eventObjs[eventType][i];
f.call(el,e);
}
};
}
};
代码比较简单,不做过多解释。也希望有兴趣的同学给出bug。
复制代码 代码如下:
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;i<len;i++){
var 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;i<len;i++){
var f = el.eventObjs[eventType][i];
f.call(el,e);
}
};
}
};
代码比较简单,不做过多解释。也希望有兴趣的同学给出bug。
上一篇:基于JQuery模仿苹果桌面的Dock效果(初级版)
栏 目:JavaScript代码
本文标题:javascript 传统事件模型构造的事件监听器实现代码
本文地址:http://www.codeinn.net/misctech/183857.html