Html 如果 IE 条件不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19502040/
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 conditionals not working
提问by TheSnooker
I have been driving myself nuts trying to get comment conditionals to work and I'm not having any luck can someone explain what I'm doing wrong?
我一直在让自己发疯,试图让评论条件起作用,我没有任何运气有人可以解释我做错了什么吗?
Here is my code:
这是我的代码:
<!--[if IE 10]>
IE IS VERSION 10<br />
<![endif]-->
<!--[if !IE]><!-->
Browser is not IE
<!--<![endif]-->
<!--[if lt IE 9]>
IE IS LESS THAN VERSION 9<br />
<![endif]-->
What is happening is frustratingly inconsistant. When I load the page with the above code in IE8it get the message "IE IS LESS THAN VERSION 9"Great right? No because when I load the SAME PAGE in IE10I get the message "Browser is not IE"
正在发生的事情令人沮丧地不一致。当我在IE8 中加载带有上述代码的页面时,它收到消息“IE 小于版本 9”,对吧?不,因为当我在IE10 中加载 SAME PAGE 时,我收到消息 “浏览器不是 IE”
Why does it think that IE10 is not an IE browser?! I've been crawling page after page but there doesn't seem to be any thing wrong with my code from what I've found.
为什么它认为IE10不是IE浏览器?!我一直在逐页爬行,但从我发现的内容来看,我的代码似乎没有任何问题。
采纳答案by Zaheer Ahmed
CSS Solution:
CSS 解决方案:
If you want to apply only CSS base on browser then you can try:
如果您只想在浏览器上应用 CSS,那么您可以尝试:
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
/* Put your IE-only styles here. Works for IS 10 & IE 11*/
}
JavaScript Solution:
JavaScript 解决方案:
IE 10 does not support conditional statements.
IE 10 不支持条件语句。
Conditional statements in Internet Explorer 10.. It will treat conditional comments as regular HTML comments, and ignored entirely.
Internet Explorer 10 中的条件语句。它将条件注释视为常规 HTML 注释,并完全忽略。
Use a feature detection library such as Modernizrinstead of browser detection.
使用功能检测库(例如Modernizr)而不是浏览器检测。
found a solution on impressivewebsin this comment:
The solution is:
解决办法是:
if (Function('/*@cc_on return document.documentMode===10@*/')()) {
alert('IE 10');
} else {
alert('Not IE 10');
}
It
它
- doesn't need conditional comments;
- works even if comment stripping compression/processing;
- no ie10 class added in Internet Explorer 11;
- more likely to work as intended with Internet Explorer 11 running in Internet Explorer 10 compatibility mode;
- doesn't need standalone script tag (can just be added to other JavaScript code in the head).
- doesn't need jQuery to test
- 不需要条件注释;
- 即使注释剥离压缩/处理也能工作;
- Internet Explorer 11 中没有添加 ie10 类;
- 在 Internet Explorer 10 兼容模式下运行的 Internet Explorer 11 更有可能按预期工作;
- 不需要独立的脚本标签(可以添加到头部的其他 JavaScript 代码中)。
- 不需要 jQuery 来测试
回答by Maximus
I'm surprised that no one has added in a css-only solution. If you just want to use css, then use a statement like this:
我很惊讶没有人添加仅 css 的解决方案。如果您只想使用 css,请使用如下语句:
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
/* Put your IE-only styles here. Works for IS 10 & IE 11*/
}
This way you don't have to rely on jquery, or any html markup. Just post it in the css and you are good to go.
这样您就不必依赖 jquery 或任何 html 标记。只需将其发布在 css 中,您就可以开始使用了。
Now, is it a hack? Likely. This depends on using the microsoft high-contrast tag, but since no other browser uses the ms tag then you should be good to go.
现在,这是一个黑客吗?可能。这取决于使用 microsoft high-contrast 标签,但由于没有其他浏览器使用 ms 标签,所以你应该很高兴。
Finally, check out these pages for more info:
最后,查看这些页面以获取更多信息:
回答by abiNerd
IE 10, 11 and upwardno longer support conditional comments.
IE 10、11及更高版本不再支持条件注释。
See this answer: https://stackoverflow.com/a/22187600/1498739
回答by Ian
Try add the following meta tag near the top of the page to opt into Internet Explorer 9 behavior:
尝试在页面顶部附近添加以下元标记以选择加入 Internet Explorer 9 行为:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
This is because conditional comments has been removed in Internet Explorer 10 standards and quirks modes for improved interoperability and compliance with HTML5. This means that Conditional Comments are now treated as regular comments, just like in other browsers. This change can impact pages written exclusively for Windows Internet Explorer or pages that use browser sniffing to alter their behavior in Internet Explorer.
这是因为 Internet Explorer 10 标准和怪癖模式中已删除条件注释,以提高互操作性和与 HTML5 的合规性。这意味着条件注释现在被视为常规注释,就像在其他浏览器中一样。此更改可能会影响专为 Windows Internet Explorer 编写的页面或使用浏览器嗅探来更改其在 Internet Explorer 中的行为的页面。
回答by sclarson
IE 10 dropped conditional comments.
IE 10 删除了条件注释。
You can do something similar in javascript like this:
你可以在 javascript 中做类似的事情:
if ($.browser.msie && $.browser.version === 10) {
// stuff here (like adding an IE10 class to the body or html tag
}