javascript 跨浏览器的一些小技巧
??? 用网页调整格式实在是太复杂了,凑合看看吧。。。。。
????在IE中
????? 如果按键代表一个字符(非Shift、Ctrl、Alt等),则keyCode将返回字符的代码,即Unicode值。
在DOM中
????? ?????? 获得字符代码用charCode?
var iCharCode = oEvent.charCode;
????? ?????? 获得字符使用??
?var sChar = String.fromCharCode(oEvent.charCode);
???????????? 如果不确定按键是否包含字符,则使用isChar来判断
???if(oEvent.isChar){
????var iCharCode = oEvent.charCode;
???}
?
?
把IE的Event事件统一成DOM的形式,创建formatEvent方法。建立EventUtil类
??
EventUtil.formatEvent = function (oEvent){ if(isIE){ oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0; oEvent.eventPhase = 2; //代表冒泡阶段,IE仅支持冒泡阶段 oEvent.isChar = (oEvent.charCode > 0); oEvent.pageX = oEvent.clientX + document.body.scrollLeft; oEvent.pageY = oEvent.clientY + document.body.scrollTop; oEvent.preventDefault = function (){ this.returnvalue = false; } if(oEvent.type == "mouseout"){ oEvent.relatedTarget = oEvent.toElement; }else if(oEvent.type == "mouseover"){ oEvent.relatedTarget = oEvent.fromElement; } oEvent.stopPropagation = function (){ this.cancelBubble = true; } oEvent.target = oEvent.srcElement; oEvent.time = (new Date).getTime(); } return oEvent; }
?
?IE
与DOM的event获取方法也不大一样。特别说明的是函数的caller属性。每个函数都有caller属性,指向调用它的方法的引用。即,funcA()调用funcB(),funcB.caller就等于funcA
?
EventUtil.getEvent = function(){ if(window.event){ return this.formatEvent(window.event); }else{ return EventUtil.getEvent.caller.arguments[0];//event对象总是事件处理函数的第一个参数。 } }
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?