網頁中區分IE各版本

來源:互聯網
上載者:User

標籤:

CSS Hack

直接在CSS檔案中寫CSS Hack是非常直觀的區分方法。區分不同IE版本的hack代碼為

#content{background:red; /* 所有瀏覽器 */background:orange\9; /* 所有IE瀏覽器 */*background:yellow; /* IE7和IE6 */+background:green; /* IE7 */_background:blue; /* IE6 */}

還有一些hack不太實用,就不一一列舉了。
CSS Hack的缺點是CSS檔案無法通過W3C驗證,代碼閱讀時會很奇怪。

 

條件注釋CSS檔案

條件注釋是寫在html裡的只會被IE識別的代碼,一般在<head>地區通過不同的條件注釋匯入不同的CSS,不同的IE瀏覽器會引用不同的CSS。下面的4段注釋依次表示"在IE下使用"、“低於IE8時使用”、“IE7時使用”、“IE6時使用”:

<!--[if IE]><link rel="stylesheet" href="/ie-all.css" type="text/css" media="screen" /><![endif]--><!--[if lt IE 8]><link rel="stylesheet" href="/ie.css" type="text/css" media="screen" /><![endif]--><!--[if IE 7]><link rel="stylesheet" href="/ie7.css" type="text/css" media="screen" /><![endif]--><!--[if IE 6]><link rel="stylesheet" href="/ie6.css" type="text/css" media="screen" /><![endif]-->

由於IE8下CSS問題較少,一般只需要為IE6、7寫一點修正代碼。如果需要,可以在ie.css裡用CSS Hack對IE6/7進行差異處理。

<!--[if lt IE 8]><link rel="stylesheet" href="/ie.css" type="text/css" media="screen" /><![endif]-->

條件注釋CSS檔案的缺點是會增加至少1次http請求,影響渲染速度,而且維護時不夠方便。

 

條件注釋<html>

條件注釋<html>跟上面的方法原理一樣,只不過這裡是給<html>注釋不同的class。

<!--[if lt IE 7 ]> <html class="ie6"> <![endif]--><!--[if IE 7 ]> <html class="ie7"> <![endif]--><!--[if IE 8 ]> <html class="ie8"> <![endif]--><!--[if IE 9 ]> <html class="ie9"> <![endif]--><!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->

這樣在CSS中使用下面的代碼即可區分版本

#content{background:red;}.ie9 #content{background:orange;}.ie8 #content{background:yellow;}.ie7 #content{background:green;}.ie6 #content{background:blue;}

你也可以增加諸如.ltie8這樣的class來實現更方便的版本分組管理。這個方法不會增加http請求,而且不需要任何hack。

 

IE中的判斷語句

上面寫的<!--[if lte IE 6]>在正常的HTML中屬於注釋,不會被執行,但在IE中是一個判斷語句,所以這些代碼只有在IE中才會被識別並載入。

lte:就是Less than or equal to的簡寫,也就是小於或等於的意思。lt :就是Less than的簡寫,也就是小於的意思。gte:就是Greater than or equal to的簡寫,也就是大於或等於的意思。gt :就是Greater than的簡寫,也就是大於的意思。! : 就是不等於的意思,跟javascript裡的不等於判斷符相同

 

網頁中區分IE各版本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.