IE 的 JavaScript 条件注释

标签 javascript internet-explorer

以下是针对 Internet Explorer 9+ 和所有非 IE 浏览器的经典条件注释:

<!--[if gt IE 8]><!-->
    IE 9+ and all non-IE browsers see this text.
<!--<![endif]-->

这 4 个部分各自的用途是什么?

<!--[if gt IE 8]>
<!-->
<!--
<![endif]-->

最佳答案

它们是带有条件语句和运算符的 HTML 注释。让我来分解一下:

<!--[if gt IE 8]><!-->

gt这里的意思是“大于,并且IE”表示 Internet Explored,以及后面的版本。这:

<!--<![endif]-->

表示结束条件语句。此特定语法不同于常规 HTML 兼容语法:

<!--[if condition]>-->
   Stuff here
<!--<![endif]-->

由于以下原因,来自SitePoint :

IE7 and later browsers will also reveal the -–> after the opening conditional statement. The fix suggested by Microsoft is to add an extra <! just after the opening conditional comment.

这意味着 -->之后<!--[if condition]>由 IE 7+ 浏览器解释。您给出的评论用于解决这一问题。阅读有关条件注释的更多信息 at Microsoft's MSDN .

关于IE 的 JavaScript 条件注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39671505/

相关文章:

javascript - 为什么 Flow 不理解可为 null 的属性与不可为 null 的对象属性兼容?

Javascript - 如何使 getTime() 在所有计算机上统一?

JavaScript .push() 内部函数正在覆盖全局变量

html - IE 1 像素太高

html - IE 仅在 CSS/设计、跨浏览器兼容性方面存在问题

jquery - 幻灯片图像链接在 IE9 中不起作用

javascript - React - 如何渲染变量中包含的 HTML?

javascript - 将具有值的子对象添加到现有的 javaScript 对象

javascript - 资源管理器 : how to highlight text after using pasteHTML method?

html - Internet Explorer 8 和边距