解决JS在firefox和IE下差异问题

开发 前端
这里和大家分享一下JS在firefox和IE下存在的差异及解决方案,比如集合类对象问题,IE下可以使用()或[]获取集合类对象,而在Firefox下只能使用[]获取集合类对象。

本文和大家重点讨论一下JS在firefox和IE下差异及解决方案,IE下,可以使用获取常规属性的方法来获取自定义属性,也可以使用getAttribute()获取自定义属性;Firefox下,只能使用getAttribute()获取自定义属性。

JS在firefox和IE下差异及解决方案

1.document.formName.item(itemName)问题

说明:

IE下,可以使用document.formName.item(itemName)或document.formName.elements[elementName];

Firefox下,只能使用document.formName.elements[elementName].
解决方法:统一使用document.formName.elements[elementName].

2.集合类对象问题

说明:IE下,可以使用()或[]获取集合类对象;Firefox下,只能使用[]获取集合类对象.
解决方法:统一使用[]获取集合类对象.

3.自定义属性问题

说明:IE下,可以使用获取常规属性的方法来获取自定义属性,也可以使用getAttribute()获取自定义属性;Firefox下,只能使用getAttribute()获取自定义属性.
解决方法:统一通过getAttribute()获取自定义属性.

4.eval(idName)问题

说明:IE下,,可以使用eval(idName)或getElementById(idName)来取得id为idName的HTML对象;Firefox下只能使用getElementById(idName)来取得id为idName的HTML对象.
解决方法:统一用getElementById(idName)来取得id为idName的HTML对象.

5.变量名与某HTML对象ID相同的问题

说明:IE下,HTML对象的ID可以作为document的下属对象变量名直接使用;Firefox下则不能.Firefox下,可以使用与HTML对象ID相同的变量名;IE下则不能。
解决方法:使用document.getElementById(idName)代替document.idName.***不要取HTML对象ID相同的变量名,以减少错误;在声明变量时,一律加上var,以避免歧义.

6.const问题

说明:Firefox下,可以使用const关键字或var关键字来定义常量;IE下,只能使用var关键字来定义常量.
解决方法:统一使用var关键字来定义常量.

7.input.type属性问题

说明:IE下input.type属性为只读;但是Firefox下input.type属性为读写.#p#

8.window.event问题

说明:window.event只能在IE下运行,而不能在Firefox下运行,这是因为Firefox的event只能在事件发生的现场使用.
解决方法:

IE: 

  1. <inputnameinputname="Button8_1"type="button"
  2. value="IE"onclick="javascript:gotoSubmit8_1()"/> 
  3. ...  
  4. <scriptlanguagescriptlanguage="javascript"> 
  5. functiongotoSubmit8_1(){  
  6. ...  
  7. alert(window.event);//usewindow.event  
  8. ...  
  9. }  
  10. </script> 
  11. IE&Firefox:  
  12. <inputnameinputname="Button8_2"type="button"
  13. value="IE"onclick="javascript:gotoSubmit8_2(event)"/> 
  14. ...  
  15. <scriptlanguagescriptlanguage="javascript"> 
  16. functiongotoSubmit8_2(evt){  
  17. ...  
  18. evtevt=evt?evt:(window.event?window.event:null);  
  19. alert(evt);//useevt  
  20. ...  
  21. }  
  22. </script> 

9.event.x与event.y问题

说明:IE下,even对象有x,y属性,但是没有pageX,pageY属性;Firefox下,even对象有pageX,pageY属性,但是没有x,y属性.
解决方法:使用mX(mX=event.x?event.x:event.pageX;)来代替IE下的event.x或者Firefox下的event.pageX.

10.event.srcElement问题

说明:IE下,even对象有srcElement属性,但是没有target属性;Firefox下,even对象有target属性,但是没有srcElement属性.
解决方法:使用obj(obj=event.srcElement?event.srcElement:event.target;)来代替IE下的event.srcElement或者Firefox下的event.target.

11.window.location.href问题

说明:IE或者Firefox2.0.x下,可以使用window.location或window.location.href;Firefox1.5.x下,只能使用window.location.
解决方法:使用window.location来代替window.location.href.

12.模态和非模态窗口问题

说明:IE下,可以通过showModalDialog和showModelessDialog打开模态和非模态窗口;Firefox下则不能.
解决方法:直接使用window.open(pageURL,name,parameters)方式打开新窗口。如果需要将子窗口中的参数传递回父窗口,可以在子窗口中使用window.opener来访问父窗口.例如:varparWin=window.opener;parWin.document.getElementById(Aqing).value=Aqing;#p#

13.frame问题

以下面的frame为例: 

  1. <framesrcframesrc="xxx.html"id="frameId"name="frameName"/> 
  2.  

(1)访问frame对象:

IE:使用window.frameId或者window.frameName来访问这个frame对象.
Firefox:只能使用window.frameName来访问这个frame对象.
另外,在IE和Firefox中都可以使用window.document.getElementById(frameId)来访问这个frame对象.

(2)切换frame内容:

在IE和Firefox中都可以使用window.document.getElementById(testFrame).src=xxx.html或window.frameName.location=xxx.html来切换frame的内容.
如果需要将frame中的参数传回父窗口,可以在frme中使用parent来访问父窗口。例如:parent.document.form1.filename.value=Aqing;

14.body问题

Firefox的body在body标签没有被浏览器完全读入之前就存在;而IE的body则必须在body标签被浏览器完全读入之后才存在.
Firefox: 

  1. <body> 
  2. <scripttypescripttype="text/javascript"> 
  3. document.body.onclick=function(evt){  
  4. evtevt=evt||window.event;  
  5. alert(evt);  
  6. }  
  7. </script> 
  8. </body> 
  9. IE&Firefox:  
  10. <body> 
  11. </body> 
  12. <scripttypescripttype="text/javascript"> 
  13. document.body.onclick=function(evt){  
  14. evtevt=evt||window.event;  
  15. alert(evt);  
  16. }</script> 

 15.事件委托方法

IE:document.body.onload=inject;//Functioninject()在这之前已被实现
Firefox:document.body.onload=inject();
有人说标准是:
document.body.onload=newFunction('inject()');

16.firefox与IE(parentElement)的父元素的区别

IE:obj.parentElement
firefox:obj.parentNode
解决方法:因为firefox与IE都支持DOM,因此使用obj.parentNode是不错选择.#p#

17.cursor:handVScursor:pointer

firefox不支持hand,但ie支持pointer
解决方法:统一使用pointer

18.innerText在IE中能正常工作,但是innerText在FireFox中却不行.

解决方法: 

  1. if(navigator.appName.indexOf(Explorer)>-1){  
  2. document.getElementById('element').innerText=mytext;  
  3. }else{  
  4. document.getElementById('element').textContent=mytext;  
  5. }  

 19.FireFox中类似obj.style.height=imgObj.height的语句无效

解决方法:
obj.style.height=imgObj.height+'px';

20.ie,firefox以及其它浏览器对于table标签的操作都各不相同。

在ie中不允许对table和tr的innerHTML赋值,使用js增加一个tr时,使用appendChile方法也不管用。

解决方法: 

  1. //向table追加一个空行:  
  2. varrow=otable.insertRow(-1);  
  3. varcell=document.createElement(td);  
  4. cell.innerHTML=;  
  5. cell.className=XXXX;  
  6. row.appendChild(cell); 

21.padding问题

padding5px4px3px1pxFireFox无法解释简写,必须改成padding-top:5px;padding-right:4px;padding-bottom:3px;padding-left:1px;

22.消除ul、ol等列表的缩进时

样式应写成:list-style:none;margin:0px;padding:0px;
其中margin属性对IE有效,padding属性对FireFox有效

23.CSS透明

IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。
FF:opacity:0.6。

24.CSS圆角

IE:不支持圆角。
FF:-moz-border-radius:4px,或者-moz-border-radius-topleft:4px;-moz-border-radius-topright:4px;-moz-border-radius-bottomleft:4px;-moz-border-radius-bottomright:4px;。

【编辑推荐】

  1. 解析JS中实现打印的两大方式
  2. JS中Array数组的三大属性用法揭秘
  3. 盘点JavaScript中Function三大用途
  4. 技术分享 如何识别控制DHTML和JS中的页面元素
  5. JavaScript调试工具解决IE6等多版本共存问题 

 

责任编辑:佚名 来源: hi.baidu.com
相关推荐

2009-06-09 21:46:18

JavaScript差IEFirefox

2010-09-16 11:08:50

JSIEFirefox

2010-08-17 15:52:59

FirefoxIEJavaScript

2010-09-01 14:51:12

CSSIEFirefox

2010-08-27 15:56:52

IEFirefoxCSS

2010-08-20 13:34:12

IEFirefoxJavascript

2010-08-17 15:21:17

IEFirefoxHTML

2010-09-15 09:21:11

IEirefoxJavascript

2010-08-19 17:06:16

IEFirefox

2010-08-18 15:02:54

IEFirefox兼容

2010-08-24 10:53:49

CSSpaddingIE

2010-08-27 15:08:10

FirefoxIE6IE7

2010-09-01 15:16:47

CSSIEFirefox

2010-08-19 09:09:53

FirefoxIECSS

2010-08-23 09:23:48

IEFirefox兼容性

2010-08-19 09:29:26

hoverIE6

2010-08-18 15:41:38

IE6E7Firefox

2010-08-27 13:31:58

IE6IE7Firefox

2010-09-15 08:41:25

IE6IE7Firefox兼容

2010-10-09 15:22:25

IE7.JS
点赞
收藏

51CTO技术栈公众号