0

我想声明一种与 ie6 和 ie7 不同的样式,但我在 css 中的条件被 IE7 识别为 IE6。我使用 XP 和 explorer 7。这是我使用的代码:

<!--[if !IE]>

#mainDiv{text-align:-moz-center;}

#skyBanner {top:0px;left:0px; position:fixed;visibility:hidden;}

<![endif]-->     

<!--[if lt IE 7]>

body > #skyBanner {  position: fixed;}

#skyBanner {position:absolute;visibility:hidden;

left: expression( ( 0 + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px' );

top: expression( ( 0 + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px' );

}

<![endif]-->

<!--[if IE 7]>

#skyBanner {position:fixed;visibility:hidden;

}    
<![endif]--> 

我的错误是什么?

4

3 回答 3

3

您不能在 CSS 中使用条件注释。仅在 HTML 中。因此,您必须将不同浏览器的声明放入不同的文件中,并对<link>它们进行条件注释。

所以更多类似的东西

<html>
  <head>
    <!--[if !IE]>
      <link rel="stylesheet" type="text/css" href="style_non_ie.css">
    <![endif]-->
    ...
  </head>
</html>
于 2009-07-12T10:42:01.057 回答
3

!IE的评论不正确,并且您缺少样式标签。如果这正是它在您的 HTML 中的存在方式,那么这就是您的问题。如果这是在 CSS 文件中,那么您不能在该位置使用条件注释。

更正:

<!--[if !IE]>-->
<style type="text/css" media="screen">
    #mainDiv {text-align:-moz-center;}
    #skyBanner {top:0px;left:0px; position:fixed;visibility:hidden;}
</style>
<!--<![endif]-->

<!--[if lt IE 7]>
<style type="text/css" media="screen">
    body > #skyBanner {  position: fixed;}
    #skyBanner {position:absolute;visibility:hidden;
    left: expression( ( 0 + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px');
    top: expression( ( 0 + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px' );
    }
</style>
<![endif]-->

<!--[if IE 7]>
<style type="text/css" media="screen">
    #skyBanner {position:fixed;visibility:hidden;}
</style>
<![endif]-->

同样,正如当前编写的那样,没有浏览器看到!IE代码。

我也不确定您是否正确编写了其他条件。您在body > #skyBanner {position: fixed;}“”条件下有“ if lt IE 7”,但据我所知,IE6 及更低版本不支持此 CSS 选择器。

因此,我所描述的任何数量的问题都可能导致您在使用 IE6 和 IE7 时遇到问题。

于 2009-07-12T10:57:39.700 回答
0

您需要以不同的方式进行操作。使用注释并附上浏览器特定 CSS 文件的链接。这样它应该可以工作:

<!--[if !IE]>
<link href="nonIE.css" rel="stylesheet" type="text/css">
<![endif]-->     

<!--[if lt IE 7]>
<link href="IE6.css" rel="stylesheet" type="text/css">
<![endif]-->

<!--[if IE 7]>
<link href="IE7.css" rel="stylesheet" type="text/css">
<![endif]-->

您也可以使用<style>标签而不是链接,但这是一种不好的做法。

于 2009-07-12T10:45:38.320 回答