手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >Firefox和IE浏览器兼容JS脚本写法小结
Firefox和IE浏览器兼容JS脚本写法小结
摘要:1.window.event兼容脚本functiongetEvent(){//获取浏览器事件,同时兼容ie和ff的写法if(document...

1.window.event兼容脚本

functiongetEvent(){//获取浏览器事件,同时兼容ie和ff的写法

if(document.all)returnwindow.event;

func=getEvent.caller;

while(func!=null){

vararg0=func.arguments[0];

if(arg0){

if((arg0.constructor==Eventarg0.constructor==MouseEvent)

(typeof(arg0)=="object"&&arg0.preventDefault&&arg0.stopPropagation)){

returnarg0;

}

}

func=func.caller;

}

returnnull;

}

每次用事件之前Firefox都需要用getEvent()获取一下,否则就是空

2.屏蔽Form提交事件

event.returnValue=false;//forIE

evt.preventDefault();//forfirefox

3.获取事件源

varsource=event.srcElement//IE

varsource=event.target//firefox

4.添加事件兼容写法

functionaddEvent(oElement,sEvent,func){

if(oElement.attachEvent){

oElement.attachEvent(sEvent,func);

}

else{

sEvent=sEvent.substring(2,sEvent.length);

oElement.addEventListener(sEvent,func,false);

}

}

用法:addEvent(window,"onload",Start);

5.Firefox注册innerText写法

//注册firefoxinnerText

HTMLElement.prototype.__defineGetter__("innerText",

function(){

varanyString="";

varchildS=this.childNodes;

for(vari=0;iif(childS[i].nodeType==1)

anyString+=childS[i].tagName=="BR"?'n':childS[i].innerText;

elseif(childS[i].nodeType==3)

anyString+=childS[i].nodeValue;

}

returnanyString;

}

);

HTMLElement.prototype.__defineSetter__("innerText",

function(sText){

this.textContent=sText;

}

);

6.长度:FireFox长度必须加“px”,IE无所谓

7.父控件下的子控件:IE是“children”,FireFox是“childNodes”

8.XmlHttp

在IE中,XmlHttp.send(content)方法的content可以为空,而firefox则不能为空,应该用send(""),否则会出现411错误

【Firefox和IE浏览器兼容JS脚本写法小结】相关文章:

js+html5通过canvas指定开始和结束点绘制线条的方法

javascript操作ul中li的方法

JavaScript中的splice()方法使用详解

些很实用且必用的小脚本代码

javascript去除空格方法小结

浏览器检测JS代码(兼容目前各大主流浏览器)

jQuery fancybox在ie浏览器下无法显示关闭按钮的解决办法

跨浏览器的设置innerHTML方法

使用RequireJS优化JavaScript引用代码的方法

制作特殊字的脚本

精品推荐
分类导航