在做網頁開發時,跨瀏覽器相容性一直是個老生常談卻又不得不正視的課題。本文以大量實例,總結了 CSS 與 JavaScript 常見的跨瀏覽器相容問題及解法。
網頁在某些瀏覽器上顯示錯位,很多時候是因為用了 float 浮動卻沒有正確閉合,這也是 div 無法自適應高度的原因之一。若父 div 沒設 float,子 div 卻設了 float,父 div 就會無法包住子 div。解法有幾種:
(1)給父 div 也設上 float;
(2)在所有子 div 後新增一個空的清除 div:
.parent{width:100px;} .son1{float:left;width:20px;} .son2{float:left;width:80px;} .clear{clear:both;margin:0;parding0;height:0px;font-size:0px;} <div class="parent"> <div class="son1"></div> <div class="son2"></div> <div class="clear"></div> </div> (3)萬能 float 閉合——把以下程式碼加入全域 CSS,給需要閉合的 div 加上 class="clearfix" 即可:
<style> /* Clear Fix */ .clearfix:after { content:"."; display:block; height:0; clear:both; visibility:hidden; } .clearfix { display:inline-block; } /* Hide from IE Mac \*/ .clearfix {display:block;} /* End hide from IE Mac */ /* end of clearfix */ </style> 其中 :after 是偽物件,IE 不支援、非 IE 瀏覽器支援,因此不影響 IE/WIN。(4)overflow:auto——在父 div 加上 overflow:auto 就能閉合,為了解決 IE 的問題可再加上 _height:1%。
.hh { -o-text-overflow:ellipsis; text-overflow:ellipsis; white-space:nowrap; overflow:hidden; }文字超出長度後會自動截斷並以省略號結尾,但要注意 Firefox 並不支援。
Firefox 不支援 hand,IE 則支援 pointer;解法是統一使用 pointer。
各瀏覽器對透明度的支援方式不同,為在 IE、Firefox、Chrome、Safari 等主流瀏覽器都能正常顯示,可以定義一個透明度 class:
.transparent{ filter:alpha(opacity=60); /*支持 IE 浏覽器*/ -moz-opacity:0.6; /*支持 FireFox 浏覽器*/ opacity:0.6; /*支持 Chrome, Opera, Safari 等浏覽器*/ }在 IE7 和 FF 中,width 不包含 padding;而在 IE6 中則包含 padding。
IE 提供的 children、childNodes 與 Firefox 的 childNodes 行為有別:Firefox 的 childNodes 會把換行和空白字元都算進子節點,而 IE 的 childNodes 和 children 不會。若要在 Firefox 下模擬 children 屬性,可以這樣做:
if (typeof(HTMLElement) != "undefined" && !window.opera) { HTMLElement.prototype.__defineGetter__("children", function() { for (var a = [], j = 0, n, i = 0; i < this.childNodes.length; i++) { n = this.childNodes[i]; if (n.nodeType == 1) { a[j++] = n; if (n.name) { if (!a[n.name]) a[n.name] = []; a[n.name][a[n.name].length] = n; } if (n.id) a[n.id] = n; } } return a; }); }window.event 只能在 IE 下使用,Firefox 的事件只能在事件發生的現場使用,必須從源處把 event 作為參數傳入。要在兩種瀏覽器通用,可寫成:
<button onclick="onClick(event)">獲得OuterHTML</button> <script type="text/javascript"> function onclick(event){ event = event || window.event; alert(event.clientX); } </script>統一使用 document.getElementById("idName") 即可相容兩者。
Firefox 可用 const 或 var 定義常量,IE 只能用 var;解法是統一使用 var。
訪問 frame 物件時,IE 可用 window.frameId 或 window.frameName,Firefox 只能用 window.frameName,但兩者都可用 window.document.getElementById("frameId")。切換 frame 內容可用 document.getElementById("testFrame").src = "xxx.html" 或 window.frameName.location = "xxx.html";要將參數傳回父窗口可用 parent。
Firefox 的 body 在 body 標籤完全讀入前就存在,IE 則必須在完全讀入後才存在。
IE 用 obj.parentElement,Firefox 用 obj.parentNode;因為兩者都支援 DOM,建議統一使用 obj.parentNode。
innerText 在 IE 可用,Firefox 則需用 textContent:
if (navigator.appName.indexOf("Explorer") > -1) { document.getElementById('element').innerText = "my text"; } else { document.getElementById('element').textContent = "my text"; } var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } elseif (window.ActiveXObject) { // IE的獲取方式 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }注意:在 IE 中 xmlhttp.send(content) 的 content 可為空,而 Firefox 不能為 null,應改用 send(""),否則會出現 411 錯誤。
這些相容性問題在今日雖然因現代瀏覽器標準化而逐漸淡化,但理解它們有助於維護老舊系統,也能讓你更透徹地認識瀏覽器背後的運作差異。