名詞express |
描述 |
expression | 由運算符、特徵和(或)值組合造成一個條件語句 |
downlevel browser | 任何瀏覽器除了IE5+,其餘低版本瀏覽器不支持條件註釋 |
uplevel browser | IE5+支持條件註釋 |
downlevel-hidden | 低版本瀏覽器會忽視條件註釋。若是表達式爲true時,IE5+會渲染HTML頁面 |
downlevel-revealed | 低版本瀏覽器通過條件註釋的解析。若是表達式爲true時,IE5+會渲染HTML頁面 |
使用條件註釋的好處數組
下列表格中展現了基本語法類型,第一個註釋是最基本的HTML註釋。表格比較並展現每一種條件註釋的不一樣語法的用法。瀏覽器
註釋類型 | 語法或可能的值 |
HTML標準註釋 | <!-- Comment content --> |
downlevel-hidden | <!--[if expression]> HTML <![endif]--> |
downlevel-revealed | <![if expression]> HTML <![endif]> |
Item | Example | 註釋 |
---|---|---|
IE | [if IE] | 對應IE的版本功能來查看該網頁 |
value | [if IE 7] | 一個整數或浮點標號對應於瀏覽器的版本。若是是與版本號匹配的瀏覽器版本,則返回true。 |
WindowsEdition | [if WindowsEdition] | Windows 7的IE8。 "WindowsEdition"對應Windows的版本功能。 |
value | [if WindowsEdition 1] | 整數對應Windows版本。若是正在使用的的值相匹配,則返回true。 |
true | [if true] | 結果始終爲true. |
false | [if false] | 結果始終爲false. |
下表描述了可用於建立條件表達式的運算符。服務器
Item | Example | 註釋 |
---|---|---|
! | [if !IE] | NOT運算符.。被放置在要素、運算符或表達式以前,扭轉表達式的布爾含義。 |
lt | [if lt IE 5.5] | 小於運算符。若是第一個參數小於第二個參數,返回true。 |
lte | [if lte IE 6] | 小於或等於運算符。若是第一個參數小於或等於第二個參數,返回true。 |
gt | [if gt IE 5] | 大於運算符。若是第一個參數大於第二個參數,返回true。 |
gte | [if gte IE 7] | 大於或等於運算符。若是第一個參數大於或等於第二個參數,返回true。 |
( ) | [if !(IE 7)] | 子表達式運算符。配合使用布爾運算符來建立更復雜的表達式。 |
& | [if (gt IE 5)&(lt IE 7)] | AND運算符。若是全部的子表達式的值爲真,返回true。 |
| | [if (IE 6)|(IE 7)] | OR運算符。若是任何一個子表達式的計算結果爲true,返回true。 |
Downlevel-hidden條件註釋學習
此示例顯示了一個低版本隱藏的條件註釋,其中包含文本。測試
1 <!--[if IE 8]> 2 <p>Welcome to Internet Explorer 8.</p> 3 <![endif]-->
1 <!--[if gte IE 7]> 2 <script> 3 alert("Congratulations! You are running Internet Explorer 7 or a later version of Internet Explorer."); 4 </script> 5 <p>Thank you for closing the message box.</p> 6 <![endif]-->
1 <![if lt IE 8]> 2 <p>Please upgrade to Internet Explorer version 8.</p> 3 <![endif]>
當比較這種類型HTML註釋時,發如今註釋塊在"<!"和 ">"以後(前)沒有連字符("--") ,所以,註釋分隔符被視爲沒法識別的HTML。由於瀏覽器不能識別Downlevel-hidden條件註釋,那麼它就什麼都不作了。
網站
版本號spa
1 <!--[if IE 5]> 2 <p>Welcome to any incremental version of Internet Explorer 5!</p> 3 <![endif]-->
1 <!--[if IE 5.0000]> 2 <p>Welcome to Internet Explorer 5.0!</p> 3 <![endif]-->
例子 翻譯
1 <!--[if IE]><p>You are using Internet Explorer.</p><![endif]--> 2 <![if !IE]><p>You are not using Internet Explorer.</p><![endif]> 3 4 <!--[if IE 7]><p>Welcome to Internet Explorer 7!</p><![endif]--> 5 <!--[if !(IE 7)]><p>You are not using version 7.</p><![endif]--> 6 7 <!--[if gte IE 7]><p>You are using IE 7 or greater.</p><![endif]--> 8 <!--[if (IE 5)]><p>You are using IE 5 (any version).</p><![endif]--> 9 <!--[if (gte IE 5.5)&(lt IE 7)]><p>You are using IE 5.5 or IE 6.</p><![endif]--> 10 <!--[if lt IE 5.5]><p>Please upgrade your version of Internet Explorer.</p><![endif]--> 11 12 <!--[if true]>You are using an <em>uplevel</em> browser.<![endif]--> 13 <![if false]>You are using a <em>downlevel</em> browser.<![endif]> 14 15 <!--[if true]><![if IE 7]><p>This nested comment is displayed in IE 7.</p><![endif]><![endif]-->