天天看點

IE && FireFox在javascript上的差別

内容來源網絡,學習:

大緻看了一遍,其中有部分不太确定,2014.06.05,現在FF和IE的版本都很高,找不到低版本的FF,是以其中部分JS方法可能現在已經支援。後續會糾錯,如果被你先發現了,歡迎評論糾錯。

參考來源:

------------------------------------------------------------------------------------

測試代碼時,發現不少IE可以運作的ajax,但在FF中報錯。 IE和Firefox(火狐)在JavaScript方面的不相容及統一方法總結如下:

1.相容firefox的 outerHTML,FF中沒有outerHtml的方法。 if (window.HTMLElement) {

HTMLElement.prototype.__defineSetter__("outerHTML",function(sHTML) { var

r=this.ownerDocument.createRange(); r.setStartBefore(this); var

df=r.createContextualFragment(sHTML); this.parentNode.replaceChild(df,this);

return sHTML; }); HTMLElement.prototype.__defineGetter__("outerHTML",function()

{ var attr; var attrs=this.attributes; var

str="<"+this.tagName.toLowerCase(); for (var i=0;i<attrs.length;i++) {

attr=attrs[i]; if(attr.specified) str+=" "+attr.name+‘="‘+attr.value+‘"‘; }

if(!this.canHaveChildren) return str+">"; return

str+">"+this.innerHTML+"</"+this.tagName.toLowerCase()+">"; });

HTMLElement.prototype.__defineGetter__("canHaveChildren",function() {

switch(this.tagName.toLowerCase()) { case "area": case "base": case "basefont":

case "col": case "frame": case "hr": case "img": case "br": case "input": case

"isindex": case "link": case "meta": case "param": return false; } return true;

}); }

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隻能在事件發生的現場使用.

解決方法: IE: ... IE&Firefox: ...

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來通路父視窗. 例如:var parWin = window.opener;

parWin.document.getElementById("Aqing").value = "Aqing";

13.frame問題

以下面的frame為例: (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: IE&Firefox:

15. 事件委托方法

IE:document.body.onload = inject; //Function inject()在這之前已被實作

Firefox:document.body.onload = inject(); 有人說标準是: document.body.onload=new

Function(‘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中卻不行.

解決方法: if(navigator.appName.indexOf("Explorer") > -1){

document.getElementById(‘element‘).innerText = "my text"; } else{

document.getElementById(‘element‘).textContent = "my text"; }

19. FireFox中類似 obj.style.height = imgObj.height 的語句無效

解決方法: obj.style.height = imgObj.height + ‘px‘;

20. IE,firefox以及其它浏覽器對于 table

标簽的操作都各不相同,在ie中不允許對table和tr的innerHTML指派,使用js增加一個tr時,使用appendChile方法也不管用。

解決方法: //向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透明

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雙線凹凸邊框

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