兼容IE6/IE7/IE8/FireFox的CSS hack

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>區別IE6、IE7、IE8、FireFox的CSS hack</title>
<style type="text/css">
<!--
#test,#note{
 margin:0 auto;
 text-align:center;
}
.note{ text-align:left; text-indent:40px;}
#test {
 width:200px;
 height:30px;
 border: 1px solid #000000;
 color:#fff;
 line-height:30px;
}
.color{
 background-color: #CC00FF;  /*所有瀏覽器都會顯示爲紫色*/
 background-color: #FF0000/9; /*IE6、IE7、IE8會顯示紅色*/
 *background-color: #0066FF;  /*IE6、IE7會變爲藍色*/  
 _background-color: #009933;  /*IE6會變爲綠色*/
}
-->
</style>
</head>

<body>
<div id="test" class="color">測試方塊 zss</div>
<div id="note">
 <strong style="color:#009933">IE6 最終爲綠色</strong>
 <strong style="color:#0066FF">IE7 最終爲藍色</strong>
 <strong style="color:#FF0000">IE8 最終爲紅色</strong>
 <strong style="color:#CC00FF">FireFox 最終變爲紫色</strong>
    <div class="note">
    今天看到hiccblogs裏面一個關於IE8的兼容,一句話很簡單,可惜只是針對IE兼容的,而且是建立在IE7的核心之上,侷限性很大。自認爲css兼容做的相對比較熟練,既然出了IE8,那就把IE8的兼容也研究一下。
    </div>
 <div class="note">CSS兼容性一直是大家頭疼的問題,IE6、IE7和FF已經讓大家夠嗆,在以前的文章中,很多這方面的知識介紹。IE8正式版發佈以來,就想在找一種區別IE8正式版的方法.在百度搜索有很多個結果,google搜索的結果更多。IE8正式版和測試版在hack上是有很大區別的.而且網絡上的方法幾乎都是相互轉載,出現頻率最高的要數那張linxz.cn的hack圖片了,可惜這張圖裏的方法對IE8正式版無效。
  經過分析和總結,有了下面的行之有效的解決方案,或許以後還會有更好更簡單的hack,但就目前來說這個方法可以解決燃眉之急.<br />
  
</div>
 
   
 <div class="note">
 <ul>
<li style="list-style:none;"><strong style="color:#009933">Simple Example:</strong></li>
<li>"/9" 例:"margin:0px auto/9;".</li>
<li>這裏的"/9"可以區別所有IE和FireFox.</li>
<li>"*" IE6、IE7可以識別.IE8、FireFox不能.</li>
<li>"_" IE6可以識別"_",IE7、IE8、FireFox不能</li>
</ul>
  </div> 
   
</div>
</body>
</html>

 

如果要在js中向head添加style節點,可以用以下方法:

function mountStyle( css, destinDoc )
{
    destinDoc = destinDoc || document;
    var head = destinDoc.getElementsByTagName('head')[0];
    var cssLink = destinDoc.createElement("style");
    if (head)
        head.appendChild(cssLink);
 cssLink.setAttribute( "type", "text/css" ); 
 // For IE
 if( cssLink.styleSheet )
 {
  cssLink.styleSheet.cssText = css; 
 }
 // For Firefox
 else
 {
  var cssText = destinDoc.createTextNode( css );
  cssLink.appendChild(cssText );
 }
};

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章