萬盛學電腦網

 萬盛學電腦網 >> 網頁制作 >> DivCSS教程 >> HTML代碼技巧:HTML注釋表達式判斷IE版本

HTML代碼技巧:HTML注釋表達式判斷IE版本

class="area">

如何讓靜態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了 ... :)

copyright © 萬盛學電腦網 all rights reserved