Html <!--[if !IE]> 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13785587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
<!--[if !IE]> not working
提问by user1516788
I'm having trouble getting
我无法获取
<!--[if !IE]>
to work. I'm wondering if it is because I have this in my document
上班。我想知道是不是因为我的文档中有这个
<!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]-->
When I add
当我添加
<!--[if !IE]><!-->
<link type="text/css" rel="stylesheet" href="/stylesheets/no-ie.css" />
<!--<![endif]-->
to my header for some reason it doesn't work. However if I add
由于某种原因到我的标题它不起作用。但是,如果我添加
<!--[if !IE]><!-->
<style>
All my css in here
</style>
<!--<![endif]-->
to the actual html page (in the header) it works. Any suggestions? Cheers
到实际的 html 页面(在标题中)它可以工作。有什么建议?干杯
Update to my question
更新我的问题
Ok, when I removed
<!-->
I only checked in IE which was working but now back in FF the no-ie.css had been applied to FF too. So I added back in the <!-->
and removed the / (and added that into the main template so the cms wouldn't add it back in) and all is working back in FF but now the stylesheet is being applied to IE!
So I tried
好的,当我删除时,
<!-->
我只检查了正在运行的 IE,但现在回到 FF,no-ie.css 也已应用于 FF。因此,我重新添加<!-->
并删除了 /(并将其添加到主模板中,因此 cms 不会将其添加回来)并且所有内容都在 FF 中工作,但现在样式表正在应用于 IE!所以我试过了
<!--[if IE]>
<link type="text/css" rel="stylesheet" href="/stylesheets/no-ie.css">
<![endif]-->
and
和
<!--[if !IE]> -->
<link type="text/css" rel="stylesheet" href="/stylesheets/no-ie.css">
<!-- <![endif]-->
And that didn't work. Basically I'm trying to get this page to work http://css-tricks.com/examples/ResponsiveTables/responsive.phpbut move the css into a stylesheet. Surely it's got to be simple. What am I missing? I'd rather not use JQuery if I don't have to. Cheers
那没有用。基本上我试图让这个页面工作http://css-tricks.com/examples/ResponsiveTables/responsive.php但将 css 移动到样式表中。当然,它必须很简单。我错过了什么?如果没有必要,我宁愿不使用 JQuery。干杯
回答by Thinking80s
<!--[if !IE]><!--><script src="zepto.min.js"></script><!--<![endif]-->
<!--[if IE]><script src="jquery-1.7.2.min.js"></script><![endif]-->
Note: These conditional comments are no longer supported from IE 10 onwards.
注意:从 IE 10 开始不再支持这些条件注释 。
回答by user1324409
Browsers other than IE treat the conditional statements as comments because they're enclosed inside comment tags.
IE 以外的浏览器将条件语句视为注释,因为它们包含在注释标签中。
<!--[if IE]>
Non-IE browsers ignore this
<![endif]-->
However, when you're targeting a browser that is NOT IE you have to use 2 comments, one before and one after the code. IE will ignore the code between them, whereas other browsers will treat it as normal code. The syntax for targeting non-IE browsers is therefore:
但是,当您的目标浏览器不是 IE 时,您必须使用 2 条注释,一条在代码之前,一条在代码之后。IE 会忽略它们之间的代码,而其他浏览器会将其视为普通代码。因此,针对非 IE 浏览器的语法是:
<!--[if !IE]-->
IE ignores this
<!--[endif]-->
Note: These conditional comments are no longer supported from IE 10 onwards.
注意:从 IE 10 开始不再支持这些条件注释。
回答by Selay
An update if somebody still reaches this page, wondering why the ie targeting doesnt work. IE 10 and onward no longer support conditional comments. From the MS official website:
如果有人仍然到达此页面,想知道为什么 ie 定位不起作用,则更新。IE 10 及以后不再支持条件注释。来自MS官网:
Support for conditional comments has been removed in Internet Explorer 10 standards and quirks modes for improved interoperability and compliance with HTML5.
Internet Explorer 10 标准和怪癖模式中删除了对条件注释的支持,以提高互操作性和与 HTML5 的合规性。
Please see here for more details: http://msdn.microsoft.com/en-us/library/ie/hh801214(v=vs.85).aspx
请参阅此处了解更多详细信息:http: //msdn.microsoft.com/en-us/library/ie/hh801214(v=vs.85).aspx
If you desperately need to target ie, you can use this jquery code to add a ie class to and then use .ie class in your css to target ie browsers.
如果您迫切需要定位 ie,您可以使用此 jquery 代码添加一个 ie 类,然后在您的 css 中使用 .ie 类来定位 ie 浏览器。
if ($.browser.msie) {
$("html").addClass("ie");
}
Update: $.browser is not available after jQuery 1.9. If you upgrade to jQuery above 1.9 or you already use it, you can include jQuery migration script after jQuery so that it adds missing parts: jQuery Migrate Plugin
更新: $.browser 在 jQuery 1.9 之后不可用。如果您升级到 jQuery 1.9 以上或已经使用它,您可以在 jQuery 之后包含 jQuery 迁移脚本,以便它添加缺失的部分: jQuery Migrate 插件
Alternatively, please check this thread for possible workarounds: browser.msie error after update to jQuery 1.9.1
或者,请检查此线程以获取可能的解决方法:browser.msie error after update to jQuery 1.9.1
回答by Francis
I use that and it works :
我使用它并且它有效:
<!--[if !IE]><!--> if it is not IE <!--<![endif]-->
回答by Francis
The Microsoft-recommended syntax for downlevel-revealed conditional “comments” is this:
微软推荐的下层显示条件“评论”的语法是这样的:
<![if !IE]>
<link type="text/css" rel="stylesheet" href="/stylesheets/no-ie.css" />
<![endif]>
These aren't comments, but they work properly.
这些不是评论,但它们可以正常工作。
回答by Munchies
For targeting IE Users:
针对 IE 用户:
<!--[if IE]>
Place Content here for Users of Internet Explorer.
<![endif]-->
For targeting all others:
针对所有其他人:
<![if !IE]>
Place Content here for Users of all other Browsers.
<![endif]>
The Conditional Comments can only be detected by Internet Explorer, all other Browsers thread it as normal Comments.
条件注释只能被 Internet Explorer 检测到,所有其他浏览器都将其作为普通注释进行处理。
To target IE 6,7 etc.. You have to use "Greater Than Equal" or "Lesser Than (Equal)" in the If Statement. Like this.
要针对 IE 6,7 等。您必须在 If 语句中使用“大于等于”或“小于(等于)”。像这样。
Greater Than or Equal:
大于或等于:
<!--[if gte IE 7]>
Place Content here for Users of Internet Explorer 7 or above.
<![endif]-->
Lesser Than:
小于:
<!--[if lt IE 6]>
Place Content here for Users of Internet Explorer 5 or lower.
<![endif]-->
回答by John Hayford
You need to put a space for the <!-- [if !IE] -->
My full css block goes as follows, since IE8 is terrible with media queries
您需要为<!-- [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] -->
回答by BorisD
First of all the right syntax is:
首先正确的语法是:
<!--[if IE 6]>
<link type="text/css" rel="stylesheet" href="/stylesheets/ie6.css" />
<![endif]-->
Try this post: http://www.quirksmode.org/css/condcom.htmland http://css-tricks.com/how-to-create-an-ie-only-stylesheet/
试试这个帖子:http: //www.quirksmode.org/css/condcom.html和 http://css-tricks.com/how-to-create-an-ie-only-stylesheet/
Another thing you can do:
你可以做的另一件事:
Check browser with jQuery:
使用 jQuery 检查浏览器:
if($.browser.msie){ // do something... }
in this case you can change css rules for some elements or add new css link reference:
在这种情况下,您可以更改某些元素的 css 规则或添加新的 css 链接引用:
read this: http://rickardnilsson.net/post/2008/08/02/Applying-stylesheets-dynamically-with-jQuery.aspx
阅读:http: //rickardnilsson.net/post/2008/08/02/Applying-stylesheets-dynamically-with-jQuery.aspx
回答by Chintu the cool kid
This is for until IE9
这是直到 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>
This is for after IE9
这是针对 IE9 之后的
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {enter your CSS here}
回答by Kapil
For IE browser:
对于 IE 浏览器:
<!--[if IE]>
<meta http-equiv="Content-Type" content="text/html; charset=Unicode">
<![endif]-->
For all non-IE browsers:
对于所有非 IE 浏览器:
<![if !IE]>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<![endif]>
For all IE greater than 8 OR all non-IE browsers :
对于所有大于 8 的 IE 或所有非 IE 浏览器:
<!--[if (gt IE 8)|!(IE)]><!--><script src="/_JS/library/jquery-2.1.1.min.js"></script><!--<![endif]-->