如何仅在IE10 +中加载插件

时间:2017-05-05 15:56:48

标签: javascript jquery

我正在使用jquery-resizable插件: https://github.com/RickStrahl/jquery-resizable。按照下面的代码加载它。

在IE10中我想忽略它。我如何只在IE11 +和其他普通浏览器中加载插件? MS不再支持条件注释......它可以是JS或JQ解决方案。

$(".mu-panel-left").resizable({
        handleSelector: ".mu-splitter",
        resizeHeight: false
    });

    $(".mu-panel-top").resizable({
        handleSelector: ".mu-splitter-horizontal",
        resizeWidth: false
    });

编辑: 那么添加CSS会更好吗

@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
     /* IE10+ only: assign X to body */
} 

,然后用JQ检查身体是否有X级,如果是,那么它是IE10 +?

2 个答案:

答案 0 :(得分:0)

如果导航器是IE10,则此代码将检查导航器是否为IE10 var var is_ie10 == true否则为false 所以你可以检查is_ie10,如果它包含false,你可以执行函数

function getIEVersion(){
    var agent = navigator.userAgent;
    var reg = /MSIE\s?(\d+)(?:\.(\d+))?/i;
    var matches = agent.match(reg);
    if (matches != null) {
        return { major: matches[1], minor: matches[2] };
    }
    return { major: "-1", minor: "-1" };
}

var ie_version =  getIEVersion();
var is_ie10 = ie_version.major == 10;

PS我们使用navigator.userAgent来确定平台,它容易受到用户的欺骗或浏览器本身的误传。最好尽可能完全避免使用特定于浏览器的代码。

有关详细信息,请查看this

答案 1 :(得分:0)

我的解决方案不容易受到欺骗。

<强> CSS

#x {margin-bottom:0;}

/* IE 10+ only */
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
    #x {margin-bottom:1px;}
}

<强> JQ

   var marg = parseInt( $('#x').css("marginBottom") );
   if (marg == 0) { alert ('not ie');}
   else {alert('ie 10');}
   // then do whatever