function addEvent(obj,type,fn){ if(obj.addEventListener){ obj.addEventListener(type,fn,false); }else if(obj.attachEvent){ obj.attachEvent(‘on‘+type,function(){ fn.call(this); }); } } function removeEvent(obj,type,fn){ if(obj.removeEventListener){ obj.removeEventListener(type,fn,false); }else if(obj.detachEvent){ obj.detachEvent(‘on‘+type,fn); } } function preDef(evt){ var e = evt || window.event; if(e.preventDefault){ e.preventDefault(); }else{ e.returnValue = false; } } //跨浏览器获取目标对象 function getTarget(evt){ if(evt.target){ return evt.target; }else if(window.event.srcElement){ return window.event.srcElement; } } //跨浏览器获取字符编码 function getCharCode(evt){ var e = evt || window.event; if(typeof e.charCode == ‘number‘){ return e.charCode; }else{ return e.keyCode; } }
原文:http://www.cnblogs.com/oceanden/p/3937848.html