如果 IE 条件不起作用 [英] IF IE conditionals not working

查看:28
本文介绍了如果 IE 条件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在发疯,试图让评论条件起作用,但我运气不佳,有人能解释一下我做错了什么吗?

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?

这是我的代码:

        <!--[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]-->

正在发生的事情令人沮丧地不一致.当我在 IE8 中加载带有上述代码的页面时,它收到消息 IE 小于版本 9" 很好,对吗?不,因为当我在 IE10 中加载 SAME PAGE 时,我收到消息浏览器不是 IE"

What is happening is frustratingly inconsistant. When I load the page with the above code in IE8 it get the message "IE IS LESS THAN VERSION 9" Great right? No because when I load the SAME PAGE in IE10 I get the message "Browser is not IE"

为什么它认为IE10不是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.

推荐答案

CSS 解决方案:

如果您只想应用基于浏览器的 CSS,那么您可以尝试:

If you want to apply only CSS base on browser then you can try:

@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {  
   /* Put your IE-only styles here. Works for IS 10 & IE 11*/
}

JavaScript 解决方案:

IE 10 不支持条件语句.

IE 10 does not support conditional statements.

互联网条件陈述探索者 10..它将条件注释视为常规 HTML 注释,并完全忽略.

Conditional statements in Internet Explorer 10.. It will treat conditional comments as regular HTML comments, and ignored entirely.

使用特征检测库,例如 Modernizr 而不是浏览器检测.

Use a feature detection library such as Modernizr instead of browser detection.

impressivewebs 中找到了解决方案www.impressivewebs.com/ie10-css-hacks/#comment-27811" rel="noreferrer">评论:

found a solution on impressivewebs in this comment:

这里是要测试的Demo

解决办法是:

if (Function('/*@cc_on return document.documentMode===10@*/')()) {
    alert('IE 10');
} else {
    alert('Not IE 10');
}

  • 不需要条件注释;
  • 即使评论剥离压缩/处理也能工作;
  • 没有在 Internet Explorer 11 中添加 ie10 类;
  • 在 Internet Explorer 10 兼容模式下运行的 Internet Explorer 11 更有可能按预期工作;
  • 不需要独立的脚本标签(可以直接添加到头部的其他 JavaScript 代码中).
  • 不需要 jQuery 来测试

这篇关于如果 IE 条件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆