在html中判斷IE瀏覽器的版本

來源:互聯網
上載者:User

標籤:

如何讓靜態HTML代碼根據不同IE版本顯示不同內容。 這裡的技巧就是利用IE的HTML注釋運算式。

HTML 的注釋格式是 <!-- Comment content  --> , IE 對HTML注釋做了一些擴充,使之可以支援條件判斷運算式:

<!--[if expression]> HTML <![endif]-->   當運算式expression 為True 的時候,顯示 HTML 內容。

例子:

view plaincopy to clipboardprint?
<!--[if IE 5]>  
<p>Welcome to Internet Explorer 5.</p>  
<![endif]-->  
<!--[if IE 5]>
<p>Welcome to Internet Explorer 5.</p>
<![endif]-->

和程式設計語言類似,這裡的運算式還支援大於(gt)、小於(lt)、 與或非 等操作符。 下面是一些例子。

[if IE]   判斷是否IE

[if IE 7]  判斷是否是IE7

[if !IE]  判斷是否不是IE

[if lt IE 5.5]  判斷是否是IE5.5 以下版本。  (<)

[if lte IE 6]   判斷是否等於IE6 版本或者以下 (<=)

[if gt IE 5]  判斷是否IE5以上版本  (> )

[if gte IE 7]  判斷是否 IE7 版本或者以上

[if !(IE 7)] 判斷是否不是IE7

[if (gt IE 5)&(lt IE 7)]   判斷是否大於IE5, 小於IE7

[if (IE 6)|(IE 7)]  判斷是否IE6 或者 IE7

程式碼範例:

view plaincopy to clipboardprint?
<!--[if IE]><p>You are using Internet Explorer.</p><![endif]-->  
<![if !IE]><p>You are not using Internet Explorer.</p><![endif]>  
  
<!--[if IE 7]><p>Welcome to Internet Explorer 7!</p><![endif]-->  
<!--[if !(IE 7)]><p>You are not using version 7.</p><![endif]-->  
  
<!--[if gte IE 7]><p>You are using IE 7 or greater.</p><![endif]-->  
<!--[if (IE 5)]><p>You are using IE 5 (any version).</p><![endif]-->  
<!--[if (gte IE 5.5)&(lt IE 7)]><p>You are using IE 5.5 or IE 6.</p><![endif]-->  
<!--[if lt IE 5.5]><p>Please upgrade your version of Internet Explorer.</p><![endif]-->  
<!--[if IE]><p>You are using Internet Explorer.</p><![endif]-->
<![if !IE]><p>You are not using Internet Explorer.</p><![endif]>

<!--[if IE 7]><p>Welcome to Internet Explorer 7!</p><![endif]-->
<!--[if !(IE 7)]><p>You are not using version 7.</p><![endif]-->

<!--[if gte IE 7]><p>You are using IE 7 or greater.</p><![endif]-->
<!--[if (IE 5)]><p>You are using IE 5 (any version).</p><![endif]-->
<!--[if (gte IE 5.5)&(lt IE 7)]><p>You are using IE 5.5 or IE 6.</p><![endif]-->
<!--[if lt IE 5.5]><p>Please upgrade your version of Internet Explorer.</p><![endif]-->
 

 註:IE5 以下的版本不支援這種注釋擴充。 不過現在也很難找到IE4了 ... :)


在html中判斷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.