【问题标题】:Best way to detect <= IE10检测 <= IE10 的最佳方法
【发布时间】:2013-06-09 04:12:11
【问题描述】:

检测 IE 的最新方法是什么?

  • 条件 cmets 不做 IE9, IE10
  • modernizr 特征检测在这里没有帮助,因为原因是外部的(我想嵌入的 mapbox 地图中的 IE 错误)
  • jQuery + migrate 插件:如果有帮助,基本步骤是什么?

是的,我对此很陌生。干杯!

【问题讨论】:

  • 你想在客户端还是服务器上检测?
  • 我会查看现有问题,例如:stackoverflow.com/questions/9900311/…
  • 为了纠正您的第一点,IE9 确实执行条件 cmets。它只是在 IE10 中停止支持它们。
  • 通常在实践中您实际上并不想要检测 IE,而是想要检测缺少特定功能的 IE。它通常是检查该功能而不是浏览器的更简洁的方法,因为它也会触发其他缺少该功能的浏览器。那么核心问题是你为什么要检查 IE?
  • @Nucleon 我不知道为什么 IE 中没有显示 mapbox 引脚。他们说他们知道这个错误。

标签: jquery css internet-explorer detection


【解决方案1】:

对 IE6-9 使用条件 cmets,对 IE10 使用一些自定义函数。例如: HTML:

<!--[if lt IE 7 ]> <html class="ie6 ie"> <![endif]-->
<!--[if IE 7 ]>    <html class="ie7 ie"> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8 ie"> <![endif]-->
<!--[if IE 9 ]>    <html class="ie9 ie"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->

JS:

if ($('html').hasClass('ie') || (Function('/*@cc_on return document.documentMode===10@*/')())){
    isIE = true;
}else{
    isIE = false;
}

或者你可以使用:

if (Function('/*@cc_on return document.documentMode===10@*/')()){
    document.documentElement.className+=' ie';
}
// Then..
if($('html').hasClass('ie')){...} // to check for IE10 and below.

其他答案侧重于专门检测 IE10,但我认为在一个地方显示完整的检测代码会有所帮助。

【讨论】:

  • 那不只是测试html元素上的一个类吗?
  • @Torr3nt - 是的,如果检测到 IE10,它上面的代码会将 ie 类添加到 html 元素。您可以像在第一个示例中那样轻松定义变量。
  • 关于那个说明,你不能只使用条件来定位 IE 吗? &lt;!--[if IE]&gt; &lt;html class="ie"&gt; &lt;![endif]--&gt;,还是IE10也不支持通用IE条件?
  • @Torr3nt - 不,IE10 不支持条件 cmets,因此您必须使用替代方法。
  • JS-if/else什么都不输出,你知道这里有什么问题吗? (html cmets 替换为实际代码,不用担心)
【解决方案2】:
    <!-- IE10 -->
    <script>
    if(navigator.userAgent.match(/MSIE 10/)){

        var headID = document.getElementsByTagName("head")[0];   
        var newLink = document.createElement('link');

        newLink.rel = 'stylesheet';
        newLink.type = 'text/css';
        newLink.href = 'css/ie10.css';

        headID.appendChild(newLink);
    } else {
       /* do something for other versions */
    }
    </script>

【讨论】:

    猜你喜欢
    • 2011-02-17
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多