var EventUtil = { addHandler: function(elem,type,handler){ if (elem.addEventListener) { elem.addEventListener(type, handler, false); }else if(elem.attachEvent){ elem.attachEvent(‘on‘+type,handler); }else{ elem["on"+type] = handler; } }, removeHandler: function(elem,type,handler){ if (elem.removeEventListener) { elem.removeEventListener(type, handler, false); }else if(elem.attachEvent){ elem.detachEvent(‘on‘+type,handler); }else{ elem["on"+type] = handler; } }, getEvent: function(event){ return event ? event : window.event; }, getTarget: function(event){ return event.target || event.srcElement; }, preventDefault: function(e){ if ( e && e.preventDefault ) e.preventDefault(); else window.event.returnValue = false; return false; } }; EventUtil.addHandler(document.getElementById(‘iphone‘),‘click‘,function(e){ alert("iPhone版暂无下载,稍后推出,程序员正在拼命加班!"); EventUtil.preventDefault(e); });
跨浏览器事件对象
时间: 2024-10-05 00:17:49