Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

html - ie conditional comments need explanation

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>
    <!--
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...