How do you get “IE 6 conditional comments” working

2020-05-09 12:26发布

问题:

i have this markup which works fine:

<div id="hd1" class="header headerNotIE6">

i am now trying to put a ie6 specific workaround so i am trying to only have this div if the browser is not IE 6. So i want this line to hit if its IE7, 8 and firefox and chrome. I tried this but it doesn't seem to work in Firefox or Chrome.

<!--[if !(IE 6)]>
    <div id="hd1" class="header headerNotIE6">
<![endif]-->

is there any "if everything but IE6" conditional comment that works in an html file ??

回答1:

To target any IE except IE6, you use the ! operator:

<!--[if !IE 6]>
    <div id="hd1" class="header headerNotIE6">
<![endif]-->

To target any IE except IE6 as well as all other browsers, you need special syntax to break out of the conditional comments so other browsers can read and parse the HTML inside, instead of seeing the entire block as one comment:

<!--[if !IE 6]><!-->
    <div id="hd1" class="header headerNotIE6">
<!--<![endif]-->

The original syntax as shown in voyager's answer, known as downlevel-revealed syntax, lacks the extra comment delimiters. However, it is invalid HTML, so to maintain document validity you should use the above syntax instead.



回答2:

What you have to use is

<![if !IE 6]>
  <div id="hd1" class="header headerNotIE6">
<![endif]>

Browsers other than IE see <!--[if !IE 6]><div id="hd1" class="header headerNotIE6"><![endif]--> as a normal comment, so they don't ever get to see the div inside.