即有条件的评论需要说明 [英] ie conditional comments need explanation
问题描述
以下代码用于可靠的跨浏览器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注释<! - - >
因此,false条件内的任何代码都会被注释掉; <! - < body class =ie6> - >
然后IE里面有自己的语法。因此,非IE浏览器只会看到一个带注释的字符串,而IE会将其视为要执行的语句。
因此,只有一个body标签显示,
更多解释
<! - [if(gt IE 9)|!(IE)]><! - >
< body>
<! - <![endif] - >
对IE来说,这就是:
<如果大于ie9,或者不是> ie> (即条件评论)
<! - > (空注释)( - >把这里停止SO破坏语法突出显示:D)
< body>
> (即条件注释)
如果您不明白为什么, IE工作...。
同样的块,对于任何其他浏览器都是这样的:
<! - [if(gt IE 9)|!(IE)]><! - > (这只是一个评论,包含文字[if(gt IE 9)|!(IE)]>< ;!)
< body>
<! - <![endif] - > (同样,只有一条评论,包含<![endif])
The following code is for reliable cross-browser IE version detection
<!--[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]-->
What I don't understand is, why it's working.Why the normal <body>
didn't overwrite the <body class="ie_9">
since it is just a normal tag, should be recognized by all the browsers.
What's the magic about
<!-->
<body>
<!--
You probably want to change !(IE)
to (!IE)
Also, the "normal" <body>
tag you're talking about is in a conditional comment. The fact that it's on a different line doesn't matter, it's still inside the conditional comment tag, so is affected as such.
Conditional comments for IE work by using normal HTML comments <!-- -->
so any code inside a "false" conditional is just commented out; <!-- <body class="ie6"> -->
IE then has its own syntax inside of that. As such, non-IE browsers just see a commented string, and IE treats it as a statement to execute.
Because of this, only one body tag shows, and only that one gets used.
More explanation of
<!--[if (gt IE 9)|!(IE)]><!-->
<body>
<!--<![endif]-->
To IE, this says:
<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)
If you don't understand why, read the paragraph starting "Conditional comments for IE work...".
This same block, to any other browser looks like this:
<!--[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]")
这篇关于即有条件的评论需要说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!