在早期 Web 開發中,Internet Explorer (IE) 與 Firefox (火狐) 的渲染引擎差異極大,導致大量 JavaScript 與 DOM 操作代碼在雙瀏覽器環境下表現不一。雖然現代瀏覽器已高度符合標準,但維護舊專案或理解歷史演進時,掌握這些經典兼容性陷阱仍具參考價值。本文系統整理 21 項核心差異,並提供標準化解決方案。
document.formName.item() 方法不被 Firefox 支援問題: IE 允許使用 `document.formName.item("itemName")` 存取表單元素,Firefox 會報錯。
解決: 統一改用標準 DOM API:
document.formName.elements["elementName"]
問題: IE 接受 `collection("key")` 或 `collection(index)`,Firefox 僅支援 `collection["key"]` 或 `collection[index]`。
解決: 全面採用方括號語法:
document.forms("formName") → document.forms["formName"]document.getElementsByName("inputName")(1) → document.getElementsByName("inputName")[1]window.event 物件不存在於 Firefox問題: IE 透過全域 `window.event` 取得事件物件,Firefox 僅在事件處理函式參數中傳遞 event 物件。
解決: 採用跨瀏覽器相容寫法:
function handleClick(e) {
e = e || window.event; // 相容 IE
var target = e.target || e.srcElement; // 相容事件目標
// 後續邏輯...
}
綁定事件時建議使用 addEventListener / attachEvent 封裝,或直接採用現代 element.onclick = function(e){...} 寫法。
event.x/y vs event.clientX/Y / event.pageX/Y問題: IE 提供 event.x、event.y;Firefox 提供 event.pageX、event.pageY(含捲軸位移)與 event.clientX、event.clientY(視口座標)。
解決: 統一使用 clientX/clientY(最通用),或封裝工具函式:
function getMousePos(e) {
e = e || window.event;
return {
x: e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft),
y: e.clientY + (document.documentElement.scrollTop || document.body.scrollTop)
};
}
id 不能直接作為全域變數問題: IE 會自動將 id="foo" 映射為 window.foo,Firefox 不支援此行為。
解決: 嚴格使用 document.getElementById("foo") 或 document.querySelector("#foo")。
eval(idName) 取元素失效解決: 同樣改用 document.getElementById(idName)。
id 衝突問題: Firefox 允許宣告與元素 id 同名的變數,IE 會衝突。
解決: 所有區域變數必須加 var/let/const 宣告,並避免與 DOM id 重名。
parentElement/children vs parentNode/childNodes問題: Firefox 遵循 DOM 標準,childNodes 包含空白文字節點;IE 的 children 僅包含元素節點。
解決:
parentNode 替代 parentElementelement.children(現代瀏覽器均支援)或 element.getElementsByTagName("*")<form><table><input/></table></form> 中 input 的 parentNode 在 IE 為 null,Firefox 為 form)nodeName vs tagName問題: 文字節點有 nodeName 但無 tagName;IE 早期版本 nodeName 行為不穩定。
解決: 判斷元素節點時優先檢查 nodeType === 1,再使用 tagName(大寫)。
問題: IE 允許用 window.frameId 或 window.frameName 存取 frame 的 window 物件;Firefox 僅支援 window.frameName。
解決:
id 與 name 屬性window.frames["frameName"] 或 document.getElementById("frameId").contentWindowwindow.frames["frameName"].location = "new.html" 或 document.getElementById("frameId").src = "new.html"getAttribute() 取得Firefox 不支援 element.customAttr 直接存取,需用 element.getAttribute("customAttr")。建議統一使用 dataset API(data-* 屬性)或 getAttribute/setAttribute。
input.type 唯讀性差異IE 下 input.type 唯讀;Firefox 允許修改。動態切換輸入框類型(如密碼/明文)時,需建立新元素替換。
getElementsByName() / document.all[name] 無法取得 div 等非表單元素改用 document.querySelectorAll('[name="xxx"]') 或 document.getElementsByTagName("div") 後過濾。
cursor: hand 非標準IE 早期支援 cursor: hand,標準寫法為 cursor: pointer。統一使用 pointer。
filter: Alpha vs opacity/* 跨瀏覽器半透明 */
.transparent {
filter: alpha(opacity=50); /* IE 8- */
-moz-opacity: 0.5; /* 早期 Firefox */
opacity: 0.5; /* 標準:Firefox 0.9+, Chrome, IE9+ */
}
動態切換時同步設定 style.opacity 與 style.filter。
expression() 僅 IE 支援去除連結虛線框:IE 用 blr: expression(this.onFocus=this.blur()),標準 CSS 用 :focus { outline: none; }。
SCROLLBAR-*)僅 IE 支援Firefox 無對應屬性。現代方案:使用 WebKit 私有偽元素 ::-webkit-scrollbar 系列,或 JavaScript 庫(如 SimpleBar)實現跨瀏覽器自訂捲軸。
border-width 顯示位置差異IE/Firefox 對零高度元素的邊框渲染不同。避免在 height: 0 元素設定底部邊框,改用 border-bottom 於正常高度容器,或採用 box-sizing: border-box 統一盒模型。
const 關鍵字 IE 不支援(ES6 前)舊環境統一使用 var,或透過 Babel 轉譯。
document.body 可用時機差異Firefox 在 <body> 標籤開始解析時即建立 document.body;IE 需待 </body> 解析完畢。操作 body 前請確保 DOMContentLoaded 或置腳本於 </body> 前。
JS 字串中直寫 & 即可(var url = "a=1&b=2"),勿寫 &;唯有在 HTML 模板(如 TPL/XSL)中才需寫實體 &。
'addEventListener' in window 判斷能力。querySelector, classList, dataset, addEventListener。es5-shim, eventlistener-polyfill 解決舊瀏覽器缺失。雖然現代項目多以 Evergreen 瀏覽器為目標,但理解這些歷史差異有助於閱讀舊代碼、排查疑難雜症,並深化對 Web 標準演進的認知。