【问题标题】:IE conditional statements not working (syntax error?)IE 条件语句不起作用(语法错误?)
【发布时间】:2012-11-21 05:18:10
【问题描述】:

假设如下代码:

<div class="content">
<div style="background:url(swoosh.jpg) no-repeat; background-size:100% 100%;" class="top">
<div style="height:42px; align:center;" id="logo">

我的目标是让背景为 swoosh.jpg 的 div 成为一个简单的 div,其中 class=top

我已经尝试让条件自己工作,但是由于某种原因(语法?)它不能正常工作。

以下是我尝试过的

<div class="content">
<!--[if !IE]>
<div class="top">
<![endif]-->
<!--[if IE]>
<div style="background:url(swoosh.jpg) no-repeat; background-size:100% 100%;" class="top">
<![endif]-->

我应该提到,除了内联 CSS 之外,我不能为这个应用程序使用任何其他东西 - 并且无法访问标题。

【问题讨论】:

  • 从 IE10 开始不再支持条件 cmets。
  • 我给出的解决方案适用于除 IE10 之外的所有 IE - 它恢复为非 IE 代码,如果您想专门针对 IE10,我认为您必须使用 javascript 解决方案:impressivewebs.com/ie10-css-hacks

标签: html internet-explorer


【解决方案1】:

如果有人仍然访问此页面,想知道为什么 ie 定位在最近的 IE 浏览器中不起作用,则更新。 IE 10 及更高版本不再支持条件 cmets。来自微软官网:

已在 Internet Explorer 中删除对条件 cmets 的支持 10 种标准和怪癖模式,用于提高互操作性和 符合 HTML5。

更多详情请看这里:http://msdn.microsoft.com/en-us/library/ie/hh801214(v=vs.85).aspx

如果您迫切需要定位 ie,您可以使用此 jquery 代码添加一个 ie 类,然后在您的 css 中使用 .ie 类来定位 ie 浏览器。

if ($.browser.msie) {
 $("html").addClass("ie");
}

【讨论】:

    【解决方案2】:

    我认为这是您想要做的事情,但如前所述,如果您有其他选择(在 IE9 中测试 - IE10 不适用于条件语句),这不是最好的做事方式:

    <![if !IE]>
    <div class="top">
    <![endif]>
    <!--[if IE]>
    <div style="background:url(swoosh.jpg) no-repeat; background-size:100% 100%;" class="top">
    <![endif]-->
    

    http://jsfiddle.net/APFZh/2/

    IE 10 定位需要一点 JS:

    <![if !IE]><!--<script>  
    if (/*@cc_on!@*/false) {  
        document.documentElement.className+=' ie10';  
    }  
    </script><!--<![endif]-->  
    

    这会将“ie10”类附加到 html 元素,但您可以在文档中写入任何您想要的内容

    http://www.impressivewebs.com/ie10-css-hacks/

    【讨论】:

    • 处理 IE 9 和 10 以及所有其他浏览器的另一种可能方法是使用 &lt;!--[if gte IE 9]&gt;&lt;!--&gt; ... ... &lt;!--&lt;![endif]--&gt;。参考stackoverflow.com/questions/6742087/…
    【解决方案3】:

    尝试添加

    <!--[if lt IE 7]><html class="ie6"><![endif]-->
    <!--[if IE 7]><html class="ie7"><![endif]-->
    <!--[if IE 8]><html class="ie8"><![endif]-->
    <!--[if gt IE 8]><!--><html><!--<![endif]-->
    

    到 HTML 文档的顶部。然后像这样使用CSS

    .content {
          color:red;
        }
    .ie6 .content {
          color:blue;
        }
    .ie7 .content {
          color:green;
        }
    

    这样,您可以将所有 CSS 保存在一个文件中,并将 IE 类放在非 IE 类旁边。

    查看来自 Paul Irish 的 doc

    【讨论】:

    • 请注意,我不能在这个应用程序中使用样式表。
    【解决方案4】:

    最好只使用 class="top" 的 div,但在单独的 IE 样式表中为其设置不同的样式,因为不建议使用内联 CSS。

    把它放在页面的头部:

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

    至于它不起作用,我建议确保您在页面顶部有一个有效的 DOCTYPE,因为 IE 对此非常挑剔。

    【讨论】:

    • 样式表不适用于当前应用程序,所有 CSS 都必须内联。
    • 至于 doctype - 我的页面实际上只是从 标记向下,并且不能有最终形式的标题(标题是由我无法控制的系统生成的) - 是否存在任何其他形式的解决方法?
    猜你喜欢
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 2011-01-08
    • 2011-12-04
    • 2012-08-15
    • 1970-01-01
    相关资源
    最近更新 更多