html - 即条件注释需要解释

标签 html internet-explorer

以下代码用于可靠的跨浏览器IE版本检测

<!--[if lt IE 7 ]><body class="ie_6"><![endif]-->
<!--[if IE 7 ]><body class="ie_7"><![endif]-->
<!--[if IE 8 ]><body class="ie_8"><![endif]-->
<!--[if IE 9 ]><body class="ie_9"><![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->
<body>
<!--<![endif]-->

我不明白的是,它为什么能正常工作。为什么正常 <body>没有覆盖 <body class="ie_9">因为它只是一个普通的标签,所以应该被所有的浏览器识别。

有什么神奇的地方

    <!-->
    <body>
    <!--

最佳答案

您可能想要更改 !(IE)(!IE)

此外,“正常”<body>您正在谈论的标签在条件评论中。它在不同的行上并不重要,它仍在条件注释标记内,因此会受到影响。

IE 的条件注释通过使用普通的 HTML 注释工作 <!-- -->所以“假”条件中的任何代码都被注释掉了; <!-- <body class="ie6"> -->然后 IE 在其中有自己的语法。因此,非 IE 浏览器只会看到注释字符串,而 IE 会将其视为要执行的语句。

因此,只有一个 body 标签显示,并且只有那个被使用。


更多解释

<!--[if (gt IE 9)|!(IE)]><!-->
<body>
<!--<![endif]-->

对于 IE,这表示:

<if greater than ie9, or not ie> (ie conditional comment)
<!--> (empty comment) (--> putting this here to stop SO ruining syntax highlighting :D)
<body>
<end if> (ie conditional comment)

如果您不明白为什么,请阅读以“IE 工作的条件注释...”开头的段落。

同样的 block ,对于任何其他浏览器来说都是这样的:

<!--[if (gt IE 9)|!(IE)]><!--> (this is just one comment, containing the text "[if (gt IE 9)|!(IE)]><!")
<body>
<!--<![endif]--> (again, just one comment, containing "<![endif]")

关于html - 即条件注释需要解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7358716/

相关文章:

javascript - Internet Explorer EDGE 兼容性问题?

css - 如何不在 IE11 上进行 border-bottom push 和不一致

powershell - 无法使用 IHTMLDocument2

jquery - CSS Body 不可滚动?

javascript - 如何使用 JavaScript 将变量的值传递到下一页?

html - 难以在 Bootstrap 3 中创建 CSS 垂直网格

html - Angular Bootstrap Accordion 动画在制作过程中无法正常工作

html - 如何在 css 中获得 5 x 5 的网格?

java - IE 浏览器中使用 https 和 spring security 的每个请求的 session ID 不断变化

javascript - IE - 如果选择框具有特定类,则阻止焦点(单击、双击)