以下代码用于可靠的跨浏览器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/