【问题标题】:Combine HTML conditional comments to include IE8+ and not IE组合 HTML 条件注释以包含 IE8+ 而不是 IE
【发布时间】:2013-06-08 20:20:41
【问题描述】:

我知道要针对 IE8+,您应该使用:

<!--[if gte IE 8]>
According to the conditional comment this is IE 8 or higher<br />
<![endif]-->

对于非 IE 浏览器,您可以使用:

<!--[if !IE]> -->
According to the conditional comment this is not IE<br />
<!-- <![endif]-->

你也可以像这样组合条件:

[if (IE 6)|(IE 7)]

但我的问题是,现在我想定位 IE8+ 或非 IE 浏览器,我尝试像这样定位它们:

<!--[if (!IE)|(gte IE 8)]> -->
This should be non IE or IE8+ browsers
<!-- <![endif]-->

这似乎适用于非 ie 浏览器,但在 IE 中添加 --&gt;

我猜这与 cmets 下层显示下层隐藏的类型有关,但我不确定如何处理它们。

【问题讨论】:

    标签: html internet-explorer browser comments conditional


    【解决方案1】:

    发件人:link

    下一个示例是相反的:“仅显示此内容,如果 浏览器不是 Internet Explorer”。

    <![if !IE]>
    Place content here to target all users not using Internet Explorer.
    <![endif]>
    

    条件 cmets 只能被 Internet Explorer 识别——其他浏览器将它们视为普通 cmets 并忽略它们。请注意,在上面的第二个示例(针对“其他”浏览器的示例)中,内容实际上并不在评论中——这就是其他浏览器显示它的原因。

    所以我认为你不能结合两种不同类型的条件 cmets

    【讨论】:

    • 啊,明白了,这把我搞糊涂了,所以,总而言之,我必须有三个条件 cmets?一个用于小于 ie8,一个用于大于,另一个用于非 ie 浏览器?
    • "用于非 ie 浏览器?" - 不,只是提供常规的 css 文件。
    • @NickR 我不明白你的意思,这会在 ie 中产生重复的内容。
    • 除非有特殊需要,否则我会这样做:1 - 适用于所有浏览器的普通样式表 2 - 适用于 IE 版本的条件 cmets
    • @Trufa 不,所以你会有一个主样式表,然后如果需要在特定版本的 IE 中覆盖,例如说 7 - 你会使用这个覆盖来修复 CSS 错误。您不需要复制整个 CSS 文件并将其提供给特定版本的 IE。也许值得了解一下您实际尝试使用单独的样式表实现的目标 - 您需要覆盖什么?
    【解决方案2】:

    这对我有用。我最终不得不两次包含非 IE-8 css:

    <!--[if lt IE 9]>
      <link rel="stylesheet" type="text/css" href="/css/ie8.css"/>
    <![endif]-->
    <!--[if gte IE 9]>
      <link rel="stylesheet" type="text/css" href="/css/non_ie8.css"/>
    <![endif]-->
    <!--[if !IE]><!-->
        <link rel="stylesheet" type="text/css" href="/css/non_ie8.css"/>
    <!--<![endif]-->
    

    见:

    http://msdn.microsoft.com/en-us/library/ms537512.ASPX

    http://css-tricks.com/how-to-create-an-ie-only-stylesheet/

    http://www.paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/

    当然,这会导致性能下降,并使代码更难维护。但是,它确实有效(至少在我尝试过的浏览器上)。

    如果你能找到一个满足你需要的 CSS 库,你可能会考虑一个好的 CSS 库。

    【讨论】:

      猜你喜欢
      • 2014-03-22
      • 2016-07-22
      • 2011-02-04
      • 2010-12-14
      • 1970-01-01
      • 2014-04-02
      • 1970-01-01
      • 2012-05-23
      • 2011-10-30
      相关资源
      最近更新 更多