<script> var events =[]; window.onload=function(){ for(var i=0;i<events.length;i++){ events[i](); } }; var o ={ addEvent:function (fn){ var i=0; for(;i<events.length;i++){ if(events[i]===fn){ break; } } if(i !=events.length ){ events.splice(i,1); } } }; //沙箱 var eventObj =(function(){ var events =[]; window.onload =function(){ }; return { addEvent:function(fn){}, removeEvent:function(fn){} }; })(); //添加一个事件处理函数 o.addEvent(function(){ alert(123); }); var f=function(){ alert(‘可以被移除的函数‘); }; o.addEvent(f); o.addEvent(function(){ alert(456); }) o.removeEvent(f); alert(‘over‘);
时间: 2024-10-20 06:23:45