JavaScript在IE和Firefox下的相容性問題

來源:互聯網
上載者:User
(一)
問題1:擷取一個元素對象的引用,在IE下,可直接使用該元素對象的id名;而在FireFox下,只能使用getElementById(idName)方法。
解決方案:統一用getElementById(idName)。 
問題2:擷取表單元素的引用,在IE下,可通過form.item('username')這種形式;而在FireFox下,只能通過form.elements['username']。(註:var form = document.getElementById("form");)
解決方案:統一用form.elements['username']這種形式。
問題3:訪問集合對象成員時,在IE下,可通過
Js代碼 Java代碼  
  1. 1. var elements = form.elements; var firstElement = elements(0);    
1. var elements = form.elements; var firstElement = elements(0);  

 而在Firefox下,只能通過
Java代碼 Java代碼  

  1. 1. var elements = form.elements; var firstElement = elements[0];    
1. var elements = form.elements; var firstElement = elements[0];  

 解決方案:統一用方括弧加下標索引來訪問。
問題4:讀取自訂屬性,在IE下,可像操作對象屬性一樣直接存取;而在FireFox下,只能通過getAttribute(attrName)方式訪問。
解決方案:統一用getAttribute(attrName)方式訪問。
問題5:常量的定義,在FireFox下能通過const關鍵詞定義,如const PI = 3.14; 而在IE下卻報錯。
解決方案:統一用var定義,如var PI = 3.14; 區別常量和變數可用大小寫字母來區別。
問題6:input元素的type屬性,在FireFox下可修改其type的值;如:
Js代碼 Java代碼  

  1. 1. var input = document.getElementById("button1");     
  2.     2. input.type = "text";  //將按鈕變成輸入框    
1. var input = document.getElementById("button1");  2. input.type = "text";  //將按鈕變成輸入框  

 但在IE下卻不能這樣使用。
解決方案:無
問題7:強制回應視窗window.showModalDialog(...)和非強制回應視窗window.showModelessDialog(...),只能在IE下運行,而在Firefox下卻用不了。
解決方案:統一用window.open(...)代替。
問題8:frame操作問題,在IE中,可通過window.frameId或者window.frameName即可獲得對frame頁面window對象的引用; 而在FireFox下,只能通過window.frameName來獲得。如下: Java代碼  

  1. Html代碼    
  2.     1. <script type="text/javascript">     
  3.     2.      //只有IE支援     
  4.     3.     //window.frameId.location = "http://www.iteye.com";     
  5.     4.     //IE,FireFox都支援     
  6.     5.     window.frameName.location = "http://www.iteye.com";     
  7.     6.     //IE,FireFox都支援     
  8.     7.     //document.getElementById("frameId").contentWindow.location = "http://www.iteye.com";     
  9.     8. </script>     
  10.     9.      
  11.     10. <iframe id="frameId" name="frameName" src="about:blank" width="750" height="500" />    
Html代碼 1. <script type="text/javascript">  2.      //只有IE支援  3.     //window.frameId.location = "http://www.iteye.com";  4.     //IE,FireFox都支援  5.     window.frameName.location = "http://www.iteye.com";  6.     //IE,FireFox都支援  7.     //document.getElementById("frameId").contentWindow.location = "http://www.iteye.com";  8. </script>  9.   10. <iframe id="frameId" name="frameName" src="about:blank" width="750" height="500" />  

 
問題9:讀取和設定一個元素內的文本,在IE下,可用innerText屬性;而在fireFox下,可用textContent屬性,兩者效果一樣。
解決方案:讓FireFox也支援innerText屬性,代碼如下:
問題10:對父元素的引用,在IE下,可用parentElement和parentNode;而在FireFox下,只能使用parentNode。
解決方案:統一用parentNode。

(二)

1、發現IE下input標籤的id屬性預設和name屬性相同,而Firefox必須明確寫出id屬性的名稱否則不能使用id屬性。
如:

Java代碼  
  1. <input type="text" name="username" value="">    
  2. 在IE下如下代碼可以執行而在Firefox下卻不可以:    
  3. <script>    
  4. alert(document.getElementById("username").value);    
  5. </script>    
  6. 必須改為如下代碼才可以:    
  7. <input type="text" name="username" id="username" value="">   
<input type="text" name="username" value=""> 在IE下如下代碼可以執行而在Firefox下卻不可以: <script> alert(document.getElementById("username").value); </script> 必須改為如下代碼才可以: <input type="text" name="username" id="username" value=""> 

以下為轉載:
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屬性為讀寫.
8.window.event問題
說明:window.event只能在IE下運行,而不能在Firefox下運行,這是因為Firefox的event只能在事件發生的現場使用. Firefox必須從源處加入event作參數傳遞。Ie忽略該參數,用window.event來讀取該event。
解決方案: Java代碼  

  1. IE&Firefox:    
  2. Submitted(event)"/> …    
  3. <script language="javascript">    
  4. function Submitted(evt) {    
  5. evt=evt?evt:(window.event?window.event:null);    
  6. }    
  7. </script>    
  8. window.open("b.html","","modal=yes,width=500,height=500,resizable=no,scrollbars=no");   
IE&Firefox: Submitted(event)"/> … <script language="javascript"> function Submitted(evt) { evt=evt?evt:(window.event?window.event:null); } </script> window.open("b.html","","modal=yes,width=500,height=500,resizable=no,scrollbars=no"); 

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下,event對象有srcElement屬性,但是沒有target屬性;Firefox下,even對象有target屬性,但是沒有srcElement屬性.
解決方案:使用obj(obj = event.srcElement ? event.srcElement : event.target;)來代替IE下的event.srcElement或者Firefox下的event.target. 請同時注意event的相容性問題。
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來訪問父視窗. 例如:var parWin = window.opener; parWin.document.getElementById("Aqing").value = "Aqing";
13.frame問題
以下面的frame為例:
<frame src="xxx.html" id="frameId" name="frameName" />
(1)訪問frame對象:
IE:使用window.frameId或者window.frameName來訪問這個frame對象. frameId和frameName可以同名。
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中的參數傳回父視窗(注意不是opener,而是parent frame),可以在frme中使用parent來訪問父視窗。例如:parent.document.form1.filename.value="Aqing";
14.body問題
Firefox的body在body標籤沒有被瀏覽器完全讀入之前就存在;而IE的body則必須在body標籤被瀏覽器完全讀入之後才存在.
15. 事件委託方法
IE:document.body.onload = inject; //Function inject()在這之前已被實現
Firefox:document.body.onload = inject();
16. firefox與IE的父元素(parentElement)的區別
IE:obj.parentElement
firefox:obj.parentNode
解決方案: 因為firefox與IE都支援DOM,因此使用obj.parentNode是不錯選擇.
17.cursor:hand VS cursor:pointer
firefox不支援hand,但ie支援pointer
解決方案: 統一使用pointer
18.innerText在IE中能正常工作,但是innerText在FireFox中卻不行. 需用textContent。
解決方案: Java代碼  

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

19. FireFox中設定HTML標籤的style時,所有位置性和字型尺寸的值必須後跟px。這個ie也是支援的。
20. ie,firefox以及其它瀏覽器對於 table 標籤的操作都各不相同,在ie中不允許對table和tr的innerHTML賦值,使用js增加一個tr時,使用appendChild方法也不管用。
解決方案: Java代碼  

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

;
21. padding 問題
padding 5px 4px 3px 1px FireFox無法解釋簡寫,
必須改成 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透明 Java代碼  

  1. IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。    
  2. FF:opacity:0.6。  
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;。
25. CSS雙線凹凸邊框 Java代碼  

  1. IE:border:2px outset;。    
  2. FF: -moz-border-top-colors: #d4d0c8 white;-moz-border-left-colors: #d4d0c8 white;-moz-border-right-colors:#404040 #808080;-moz-border-bottom-colors:#404040 #808080;   
IE:border:2px outset;。 FF: -moz-border-top-colors: #d4d0c8 white;-moz-border-left-colors: #d4d0c8 white;-moz-border-right-colors:#404040 #808080;-moz-border-bottom-colors:#404040 #808080; 

26. 對select的options集合操作
枚舉元素除了[]外,selectName.options.item()也是可以的, 另外selectName.options.length, selectName.options.add/remove都可以在兩種瀏覽器上使用。注意在add後賦值元素,否則會失敗(本人實驗如此)。
27. XMLHTTP的區別 Java代碼  

  1. //mf    
  2. if (window.XMLHttpRequest) //mf    
  3. {    
  4. xmlhttp=new XMLHttpRequest()    
  5. xmlhttp.    
  6. xmlhttp.open("GET",url,true)    
  7. xmlhttp.send(null)    
  8. }    
  9. //ie    
  10. else if (window.ActiveXObject) // code for IE    
  11. {    
  12. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")    
  13. if (xmlhttp)    
  14. {    
  15. xmlhttp.    
  16. xmlhttp.open("GET",url,true)    
  17. xmlhttp.send()    
  18. }    
  19. }    
  20. }   
//mf if (window.XMLHttpRequest) //mf { xmlhttp=new XMLHttpRequest() xmlhttp. xmlhttp.open("GET",url,true) xmlhttp.send(null) } //ie else if (window.ActiveXObject) // code for IE { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") if (xmlhttp) { xmlhttp. xmlhttp.open("GET",url,true) xmlhttp.send() } } } 

28. innerHTML的區別
Firefox不支援innerHTML, 解決辦法可以如下 Java代碼  

  1. rng = document.createRange();    
  2. el = document.getElementById(elementid);    
  3. rng.setStartBefore(el);    
  4. htmlFrag = rng.createContextualFragment(content);    
  5. while (el.hasChildNodes()) //清除原有內容,加入新內容    
  6. el.removeChild(el.lastChild);    
  7. el.appendChild(htmlFrag);   
rng = document.createRange(); el = document.getElementById(elementid); rng.setStartBefore(el); htmlFrag = rng.createContextualFragment(content); while (el.hasChildNodes()) //清除原有內容,加入新內容 el.removeChild(el.lastChild); el.appendChild(htmlFrag); 

29. img的src重新整理問題
在IE 下可以用<img id="pic" onclick= "this.src= 'aa.php'" src="aa.php" style="cursor: pointer"/> 可以重新整理圖片,但在FireFox下不行。主要是緩衝問題,在地址後面加個隨機數就解決了。編輯onclick事件代碼如下: "this.src=this.src+'?'+Math.random()"

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.