日期:2014-05-16 浏览次数:20354 次
js 实现多事件绑定,既适i用于自定义事件,也可以适用于系统事件,而且支持参数传递 源代码:
function createFunction(obj,func){ var args=[]; if(!obj){ obj=window;} for(var i=2;i<arguments.length;i++){ args.push(arguments[i]) } return function(){ obj[func].apply(obj,args); } } function eventDemo(){ } eventDemo.prototype={ show:function(){ if(this.events){ for(var i=0;i<this.events.length;i++){ this.events[i](); } } }, attachEvents:function(eventHandler){ if (!this.events) this.events=[]; this.events.push(eventHandler); } } var obj=new eventDemo(); var userName1="jake1"; var userName2="jake2"; function objOnShow1(userName){ alert(userName+" execute a onShow event"); } function objOnShow2(userName){ alert(userName+" execute a onShow event"); } obj.attachEvents(createFunction(null,"objOnShow1",userName1)); obj.attachEvents(createFunction(null,"objOnShow2",userName2)); obj.show();