HTML 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]-->