define(function(){ var Event = { //页面加载完成后注册onload事件 readyEvent:function(fn){ if(fn == null){ fn = document; } var oldonload = window.onload; if(typeof window.onload != 'function'){ window.onload = fn; }else{ // 如果window.onload上已有事件绑定,则追加到它的后面 window.onload = function(){ oldonload(); fn(); } } }, bindEvent:function(target,type,handler){ if(target.addEventListener){ target.addEventListener(type,handler,false); }else if(target.attachEvent){ target.attachEvent('on'+type,function(event){ // IE attachEvent注册的事件作为函数调用,其this值是全局对象,需要用call来修改其指向 return handler.call(target,event) }) }else{ target['on'+type] = handler; } }, // 移除事件 removeEvent:function(target,type,handler){ if(target.removeEventListener){ target.removeEventListener(type,handler,false); }else if(target.detachEvent){ target.detachEvent('on'+type,handler); }else{ target['on'+type] = null; } }, // 阻止事件传播 stopPropagation:function(event){ if(event.stopPropagation){ event.stopPropagation(); }else{ event.cancelBubble = true; } }, // 取消事件默认行为 preventDefault:function(event){ if(event.preventDefault){ event.preventDefault(); }else{ event.returnValue = false; } }, // 获取事件目标 getTarget:function(event){ return event.target || event.srcElement; }, // 获取事件对象的引用 getEvent:function(event){ var event = event || window.event; if(!event){ // caller用于在函数内部获取调用getEvent的函数 var c = this.getEvent.caller; while(c){ event = c.arguments[0]; if(event && Event == event.constructor){ break; } c = c.caller; } } return event; } }
return Event; })
|