自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

解決JS在firefox和IE下差異問題

開發(fā) 前端
這里和大家分享一下JS在firefox和IE下存在的差異及解決方案,比如集合類對象問題,IE下可以使用()或[]獲取集合類對象,而在Firefox下只能使用[]獲取集合類對象。

本文和大家重點討論一下JS在firefox和IE下差異及解決方案,IE下,可以使用獲取常規(guī)屬性的方法來獲取自定義屬性,也可以使用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].
解決方法:統(tǒng)一使用document.formName.elements[elementName].

2.集合類對象問題

說明:IE下,可以使用()或[]獲取集合類對象;Firefox下,只能使用[]獲取集合類對象.
解決方法:統(tǒng)一使用[]獲取集合類對象.

3.自定義屬性問題

說明:IE下,可以使用獲取常規(guī)屬性的方法來獲取自定義屬性,也可以使用getAttribute()獲取自定義屬性;Firefox下,只能使用getAttribute()獲取自定義屬性.
解決方法:統(tǒng)一通過getAttribute()獲取自定義屬性.

4.eval(idName)問題

說明:IE下,,可以使用eval(idName)或getElementById(idName)來取得id為idName的HTML對象;Firefox下只能使用getElementById(idName)來取得id為idName的HTML對象.
解決方法:統(tǒng)一用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關(guān)鍵字或var關(guān)鍵字來定義常量;IE下,只能使用var關(guān)鍵字來定義常量.
解決方法:統(tǒng)一使用var關(guān)鍵字來定義常量.

7.input.type屬性問題

說明:IE下input.type屬性為只讀;但是Firefox下input.type屬性為讀寫.#p#

8.window.event問題

說明:window.event只能在IE下運行,而不能在Firefox下運行,這是因為Firefox的event只能在事件發(fā)生的現(xiàn)場使用.
解決方法:

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.模態(tài)和非模態(tài)窗口問題

說明:IE下,可以通過showModalDialog和showModelessDialog打開模態(tài)和非模態(tài)窗口;Firefox下則不能.
解決方法:直接使用window.open(pageURL,name,parameters)方式打開新窗口。如果需要將子窗口中的參數(shù)傳遞回父窗口,可以在子窗口中使用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內(nèi)容:

在IE和Firefox中都可以使用window.document.getElementById(testFrame).src=xxx.html或window.frameName.location=xxx.html來切換frame的內(nèi)容.
如果需要將frame中的參數(shù)傳回父窗口,可以在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()在這之前已被實現(xiàn)
Firefox:document.body.onload=inject();
有人說標準是:
document.body.onload=newFunction('inject()');

16.firefox與IE(parentElement)的父元素的區(qū)別

IE:obj.parentElement
firefox:obj.parentNode
解決方法:因為firefox與IE都支持DOM,因此使用obj.parentNode是不錯選擇.#p#

17.cursor:handVScursor:pointer

firefox不支持hand,但ie支持pointer
解決方法:統(tǒng)一使用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等列表的縮進時

樣式應(yīng)寫成: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中實現(xiàn)打印的兩大方式
  2. JS中Array數(shù)組的三大屬性用法揭秘
  3. 盤點JavaScript中Function三大用途
  4. 技術(shù)分享 如何識別控制DHTML和JS中的頁面元素
  5. JavaScript調(diào)試工具解決IE6等多版本共存問題 

 

責任編輯:佚名 來源: hi.baidu.com
相關(guān)推薦

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-20 13:34:12

IEFirefoxJavascript

2010-08-27 15:56:52

IEFirefoxCSS

2010-09-15 09:21:11

IEirefoxJavascript

2010-08-17 15:21:17

IEFirefoxHTML

2010-08-24 10:53:49

CSSpaddingIE

2010-08-18 15:02:54

IEFirefox兼容

2010-08-19 17:06:16

IEFirefox

2010-08-27 15:08:10

FirefoxIE6IE7

2010-09-01 15:16:47

CSSIEFirefox

2010-08-19 09:29:26

hoverIE6

2010-08-23 09:23:48

IEFirefox兼容性

2010-08-18 15:41:38

IE6E7Firefox

2010-08-27 13:31:58

IE6IE7Firefox

2010-09-15 08:41:25

IE6IE7Firefox兼容

2010-08-19 09:09:53

FirefoxIECSS

2010-10-09 15:22:25

IE7.JS
點贊
收藏

51CTO技術(shù)棧公眾號