1. document.form.item 問題
問題:
現有代碼中存在許多 document.formName.item("itemName") 這樣的語句,不能在Firefox下運行
解決方法:
統一使用 document.formName.elements["elementName"]
2. 集合類對象問題
問題:
IE下,可以使用()或[]獲取集合類對象;Firefox下,只能使用[]獲取集合類對象
解決方法:
改用[ ]作為下標運算。如:document.forms("formName") 改為 document.forms["formName"]
又如:document.getElementsByName("inputName")(1) 改為 document.getElementsByName("inputName")[1]
3. event
獲取event問題:
window.event只能在IE下運行,而不能在Firefox下運行,這是因為Firefox的event只能在事件發生的現場使用。
解決方法:
在IE中,不能把Event對象作為參數傳遞給事件處理程序,只能用window.event或者event來引用Event對象。
Firefox中獲取event的方法:
(1) 從HTML頁面傳遞參數event
(2) event = arguments.callee.caller.arguments[0];
function getEvent(evt) { evt=evt?evt:(window.event?window.event:null); }
event屬性問題:
IE下,event對象有x,y屬性,但是沒有pageX,pageY屬性;Firefox下,event對象有pageX,pageY屬性,但是沒有x,y屬性。
Firefox中的event.pageX相當於IE中的event.x
解決方法:event.x = event.x ? event.x : event.pageX;
其它:
event.layerX 在IE與火狐中都有,具體意義有無差別尚未試驗。
4. HTML 對象的 id 作為對象名的問題
問題:
在IE中,HTML對象的ID可以作為document的下屬對象變量名直接使用,而在Firefox中不能。
解決方法:
統一用 getElementById("idName") 代替 idName 作為對象變量使用。
5. 用idName字符串取得對象的問題
問題:
在IE中,利用eval(idName)可以取得id為idName的HTML對象,而在Firefox中不能。
解決方法:
統一使用 getElementById(idName) 代替 eval(idName)
6. 變量名與某 HTML 對象 id 相同的問題
問題:
IE中HTML對象的ID可以作為document的下屬對象變量名直接使用;而Firefox則不能。在Firefox中,使用與HTML對象ID相同的變量名;IE下則不能。
解決方法:
統一使用使用document.getElementById("idName")代替document.idName
在聲明變量時,一律加上 var ,以避免歧義。
此外,最好不要取與 HTML 對象 id 相同的變量名,以減少錯誤。
7. frame問題
問題:
在 IE中 可以用window.testFrame取得該frame,而Firefox中不行
解決方法:
在frame的使用方面火狐和ie的最主要的區別是:
<frame src="xx.htm" id="frameId" name="frameName" />
IE可以通過id或者name訪問這個frame對應的window對象
而Firefox只可以通過name來訪問這個frame對應的window對象
如果上述frame標簽寫在最上層的window里面的htm里面,那么可以這樣訪問
IE:window.top.frameId或者window.top.frameName來訪問這個window對象
Firefox:只能這樣window.top.frameName來訪問這個window對象
另外,在火狐和IE中都可以使用window.top.document.getElementById("frameId")來訪問frame標簽
並且可以通過window.top.document.getElementById("testFrame").src = 'xx.htm'來切換frame的內容
也都可以通過window.top.frameName.location = 'xx.htm'來切換frame的內容
9. 在Firefox中,自己定義的屬性必須getAttribute()取得
if(document.all){ //IE下為dlg對象添加事件
dlg.setAttribute("onmousedown", function(){ move_Div(this); });
}else{ //Firefox下為dlg對象添加事件
dlg.setAttribute("onmousedown", "move_Div(this);");
}
10.在Firefox中沒有 parentElement children 而用 parentNode childNodes
childNodes的下標的含義在IE和Firefox中不同,Firefox使用DOM規范,childNodes中會插入空白文本節點 ---- firefox下childNodes會把換行和空白字符都算作父節點的子節點,
而ie的childNodes和children不會。
一般可以通過node.getElementsByTagName()來回避這個問題。
當html中節點缺失時,IE和Firefox對parentNode的解釋不同,例如
<form>
<table>
<input/>
</table>
</form>
Firefox中input.parentNode的值為form, 而IE中input.parentNode的值為空節點
Firefox中節點沒有removeNode方法,必須使用如下方法 node.parentNode.removeChild(node)
11.const 問題
問題:
在 IE 中不能使用 const 關鍵字。如 const constVar = 32; 在IE中這是語法錯誤。
方法:
不使用 const ,以 var 代替。
12. body 對象
Firefox的body在body標簽沒有被瀏覽器完全讀入之前就存在,而IE則必須在body完全被讀入之后才存在
13. url encoding
在JS中如果書寫url就直接寫&不要用&
如果 var url = 'xx.jsp?objectName=xx&objectEvent=xxx';
那么frm.action = url時,url很有可能不會被正常顯示以至於參數沒有正確的傳到服務器,一般會服務器報錯參數沒有找到。
當然在tpl中例外,因為tpl中符合xml規范,要求&書寫為&
一般Firefox無法識別JS中的&
14. nodeName 和 tagName 問題
問題:
在Firefox中,所有節點均有 nodeName 值,但 textNode 沒有 tagName 值。在 IE 中,nodeName 的使用好象有問題。
解決方法:
使用 tagName,但應檢測其是否為空。
15. input.type屬性問題
IE下 input.type屬性為只讀,但是Firefox下可以修改
16. document.getElementsByName() 和 document.all[name] 的問題
問題:
在 IE 中,getElementsByName()、document.all[name] 均不能用來取得 div 元素(是否還有其它不能取的元素還不知道)。
17.event.srcElement問題
問題:IE下,even對象有srcElement屬性,但是沒有target屬性;Firefox下,even對象有target屬性,但是沒有srcElement屬性。
解決方法:使用obj(obj = event.srcElement ? event.srcElement : event.target;)來代替IE下的event.srcElement或者Firefox下的event.target
18.模態和非模態窗口問題
問題:IE下,可以通過showModalDialog和showModelessDialog打開模態和非模態窗口;Firefox下則不能。
解決方法:
直接使用window.open(pageURL,name,parameters)方式打開新窗口。如果需要將子窗口中的參數傳遞回父窗口,可以在子窗口中使用window.opener來訪問父窗口。
例如:var parentWin = window.opener;
parentWin.document.getElementById("Aqing").value = "Aqing";
19. 事件委托方法
IE:document.body.onload = inject; //Function inject()在這之前已被實現
Firefox:document.body.onload = inject();
有人說標准是:document.body.onload=new Function('inject()');
20. firefox與IE(parentElement)的父元素的區別
IE:obj.parentElement
firefox:obj.parentNode
解決方法: 因為firefox與IE都支持DOM,因此使用obj.parentNode是不錯選擇。
21.cursor:hand VS cursor:pointer
Firefox不支持hand,但IE支持pointer
解決方法: 統一使用pointer
22.innerText在IE中能正常工作,但是innerText在FireFox中卻不行.
解決方法:
if(navigator.appName.indexOf("Explorer") > -1){
document.getElementById('element').innerText = "my text";
} else{
document.getElementById('element').textContent = "my text";
}
23. FireFox中類似 obj.style.height = imgObj.height 的語句無效
解決方法:obj.style.height = imgObj.height + 'px';
24. 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);
25. padding 問題
FireFox無法解釋padding 5px 4px 3px 1px 簡寫,必須改成 padding-top:5px; padding-right:4px; padding-bottom:3px; padding-left:1px;
26. 消除ul、ol等列表的縮進時
樣式應寫成:list-style:none;margin:0px;padding:0px;
其中margin屬性對IE有效,padding屬性對FireFox有效
27. CSS透明
IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)
FireFox:opacity:0.6
28. CSS圓角
IE:不支持圓角。
FireFox: -moz-border-radius:4px,或者-moz-border-radius-topleft:4px;-moz-border- radius-topright:4px;-moz-border-radius-bottomleft:4px;
-moz-border-radius- bottomright:4px;
29. CSS雙線凹凸邊框
IE:border:2px outset;
FireFox:-moz-border-top-colors: #d4d0c8 white;-moz-border-left-colors: #d4d0c8 white;-moz-border-right-colors:#404040 #808080;
-moz-border-bottom-colors:#404040 #808080;
30.window.location.href問題
問題:IE或者Firefox2.0.x下,可以使用window.location或window.location.href;Firefox1.5.x下,只能使用window.location
解決方法:使用window.location來代替window.location.href
31.模態窗口關閉
問題:FireFox不支持window.close().
解決辦法:使用window.top.close()