时间:2012-12-09 07:38:24

标签: html internet-explorer conditional-comments

我无法获得

<!--[if !IE]>

上班。我想知道是不是因为我在我的文件中有这个

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

当我添加

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

到我的标题由于某种原因它不起作用。但是,如果我添加

<!--[if !IE]><!-->
    <style>
        All my css in here
    </style>
    <!--<![endif]-->

到实际的html页面(在标题中)它的工作原理。 有什么建议? 干杯

更新我的问题

好的,当我删除 <!-->我只在IE中检查了哪个有效,但现在回到FF中,no-ie.css也已应用于FF。所以我在<!-->中添加了并删除了/(并将其添加到主模板中以便cms不会将其添加回来)并且所有都在FF中工作但现在样式表正在应用于IE ! 所以我试过

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

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

这不起作用。 基本上我试图让这个页面工作http://css-tricks.com/examples/ResponsiveTables/responsive.php但是将css移动到样式表中。当然,它必须简单。我错过了什么?如果我不需要,我宁愿不使用JQuery。 干杯

16 个答案:

答案 0 :(得分:223)

<!--[if !IE]><!--><script src="zepto.min.js"></script><!--<![endif]-->
<!--[if IE]><script src="jquery-1.7.2.min.js"></script><![endif]-->

注意:这些条件评论为no longer supported from IE 10 onwards

答案 1 :(得分:65)

IE之外的浏览器将条件语句视为注释,因为它们包含在注释标记内。

<!--[if IE]>
Non-IE browsers ignore this
<![endif]-->

但是,当你的目标浏览器不是IE时,你必须使用2条评论,一条在代码之前,一条在评论之后。 IE将忽略它们之间的代码,而其他浏览器会将其视为普通代码。因此,定位非IE浏览器的语法是:

<!--[if !IE]-->
IE ignores this
<!--[endif]-->

注意:这些条件评论为no longer supported from IE 10 onwards

答案 2 :(得分:37)

如果有人仍然到达此页面,则会更新,想知道为什么ie定位不起作用。 IE 10及以后版本不再支持条件评论。来自MS官方网站:

  

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

请点击此处了解详情:http://msdn.microsoft.com/en-us/library/ie/hh801214(v=vs.85).aspx

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

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

更新:$ .browser在jQuery 1.9之后不可用。如果你升级到1.9以上的jQuery或你已经使用它,你可以在jQuery之后包含jQuery迁移脚本,以便它添加缺少的部分: jQuery Migrate Plugin

或者,请查看此主题以获取可能的解决方法:browser.msie error after update to jQuery 1.9.1

答案 3 :(得分:13)

我使用它并且它有效:

<!--[if !IE]><!--> if it is not IE <!--<![endif]-->

答案 4 :(得分:12)

微软推荐的下层显示条件“评论”的语法是这样的:

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

这些不是评论,但它们可以正常运作。

答案 5 :(得分:8)

您需要为<!-- [if !IE] -->我的完整css块添加一个空格,如下所示,因为IE8对于媒体查询非常糟糕

<!-- IE 8 or below -->   
<!--[if lt IE 9]>
<link rel="stylesheet" type="text/css"  href="/Resources/css/master1300.css" />        
<![endif]-->
<!-- IE 9 or above -->
<!--[if gte IE 9]>
<link rel="stylesheet" type="text/css" media="(max-width: 100000px) and (min-width:481px)"
    href="/Resources/css/master1300.css" />
<link rel="stylesheet" type="text/css" media="(max-width: 480px)"
    href="/Resources/css/master480.css" />        
<![endif]-->
<!-- Not IE -->
<!-- [if !IE] -->
<link rel="stylesheet" type="text/css" media="(max-width: 100000px) and (min-width:481px)"
    href="/Resources/css/master1300.css" />
<link rel="stylesheet" type="text/css" media="(max-width: 480px)"
    href="/Resources/css/master480.css" />
<!-- [endif] -->

答案 6 :(得分:8)

定位IE用户:

<!--[if IE]>
Place Content here for Users of Internet Explorer.
<![endif]-->

定位所有其他人:

<![if !IE]>
Place Content here for Users of all other Browsers.
<![endif]>

条件注释只能由Internet Explorer检测,所有其他浏览器都将其作为正常注释进行处理。

目标IE 6,7等。你必须在If语句中使用“大于等于”或“小于(等于)”。像这样。

大于或等于:

<!--[if gte IE 7]>
Place Content here for Users of Internet Explorer 7 or above.
<![endif]-->

小于:

<!--[if lt IE 6]>
Place Content here for Users of Internet Explorer 5 or lower.
<![endif]-->

Source: mediacollege.com

答案 7 :(得分:7)

首先,正确的语法是:

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

试试这篇文章: http://www.quirksmode.org/css/condcom.htmlhttp://css-tricks.com/how-to-create-an-ie-only-stylesheet/

你可以做的另一件事:

使用jQuery检查浏览器:

if($.browser.msie){ // do something... }

在这种情况下,您可以更改某些元素的css规则或添加 新的css链接参考:

请阅读:http://rickardnilsson.net/post/2008/08/02/Applying-stylesheets-dynamically-with-jQuery.aspx

答案 8 :(得分:6)

直到IE9

<!--[if IE ]>
<style> .someclass{
    text-align: center;
    background: #00ADEF;
    color: #fff;
    visibility:hidden;  // in case of hiding
       }
#someotherclass{
    display: block !important;
    visibility:visible; // in case of visible

}
</style>

这适用于IE9之后

  @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {enter your CSS here}

答案 9 :(得分:5)

对于IE浏览器:

<!--[if IE]>
<meta http-equiv="Content-Type" content="text/html; charset=Unicode">
<![endif]-->

对于所有非IE浏览器:

<![if !IE]>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<![endif]>

对于所有大于8的IE或所有非IE浏览器:

<!--[if (gt IE 8)|!(IE)]><!--><script src="/_JS/library/jquery-2.1.1.min.js"></script><!--<![endif]-->

答案 10 :(得分:3)

条件评论是以<!--[if IE]>开头的评论,除IE之外的任何浏览器都无法读取。

从2011年起,微软宣布从IE 10开始,不支持条件评论https://msdn.microsoft.com/en-us/library/hh801214(v=vs.85).aspx

使用条件评论的唯一选择是请求IE以IE 9运行您的网站,该网站支持条件评论。

你可以自己编写自己的css和/或js文件,其他浏览器不会加载或执行它。

此代码段显示如何使IE 10或11运行ie-only.css和ie-only.js,其中包含自定义代码以解决IE兼容性问题。

    <html>
      <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
      <!--[if IE]>
            <meta http-equiv="Content-Type" content="text/html; charset=Unicode">
            <link rel="stylesheet" type="text/css" href='/css/ie-only.css' />
            <script src="/js/ie-only.js"></script>
      <![endif]-->
    </html>

答案 11 :(得分:1)

对于我来说,这适用于所有超过版本6的浏览器(IE7,8,9,10等,Chrome 3到目前为止,FF版本高达现在的版本):

//test if running in IE or not
        function isIE () {
            var myNav = navigator.userAgent.toLowerCase();
            return (myNav.indexOf('msie') != -1 || myNav.indexOf('trident') != -1) ? true : false;
        }

答案 12 :(得分:0)

如果您正在使用IE 10或更高版本,如http://tanalin.com/en/articles/ie-version-js/中所述,则不再支持条件注释。 您可以将https://gist.github.com/jasongaylord/5733469称为替代方法,也可以从navigator.userAgent检查Trident版本。如果浏览器在兼容模式下工作,也会进行验证。

答案 13 :(得分:0)

我正在使用此javascript来检测IE浏览器

if (
    navigator.appVersion.toUpperCase().indexOf("MSIE") != -1 ||
    navigator.appVersion.toUpperCase().indexOf("TRIDENT") != -1 ||
    navigator.appVersion.toUpperCase().indexOf("EDGE") != -1
)
{
    $("#ie-warning").css("display", "block");
}

答案 14 :(得分:0)

谢谢Fernando68,我用过这个:

function isIE () {
     var myNav = navigator.userAgent.toLowerCase();
     return (myNav.indexOf('msie') != -1 || myNav.indexOf('trident') != -1) ? true : false;
}

if(isIE()){
     $.getScript('js/script.min.js', function() {
            alert('Load was performed.');
     });
}

答案 15 :(得分:-1)

我在浏览器上运行此代码:

<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->

注意此代码:HTML5 Shiv和Respond.js IE8支持HTML5元素和媒体查询

相关问题