Internet Explorer 的條件註釋

條件註釋可用於自定義不同版本的 Microsoft Internet Explorer 的程式碼。例如,可以提供不同的 HTML 類,指令碼標記或樣式表。Internet Explorer 版本 5 到 9 支援條件註釋。較舊和較新的 Internet Explorer 版本以及所有非 IE 瀏覽器都被視為下層,並將條件註釋視為普通 HTML 註釋。

下層隱藏

下層隱藏的註釋通過將整個內容封裝在看似普通的 HTML 註釋中來工作。只有 IE 5 到 9 仍然會將其作為條件註釋讀取,並且它們將相應地隱藏或顯示內容。在其他瀏覽器中,內容將被隱藏。

<!--[if IE]>
  Revealed in IE 5 through 9. Commented out and hidden in all other browsers.
<![endif]-->

<!--[if lt IE 8]>
  Revealed only in specified versions of IE 5-9 (here, IE less than 8).
<![endif]-->

<!--[if !IE]>
  Revealed in no browsers. Equivalent to a regular HTML comment.
<![endif]-->

<!--
  For purposes of comparison, this is a regular HTML comment.
-->

下層 -透露

這些與下層隱藏的註釋略有不同:只有條件註釋本身包含在正常註釋語法中。不支援條件註釋的瀏覽器將忽略它們並在它們之間顯示其餘內容。

<!--[if IE]>-->
  The HTML inside this comment is revealed in IE 5-9, and in all other browsers.
<!--<![endif]-->

<!--[if IE 9]>-->
  This is revealed in specified versions of IE 5-9, and in all other browsers.
<!--<![endif]-->

<!--[if !IE]>-->
  This is not revealed in IE 5-9. It's still revealed in other browsers.
<!--<![endif]-->