简体   繁体   English

jQuery noConflict IE问题

[英]jQuery noConflict IE issue

I'm building a widget that depends on jquery being loaded - I'm using the following to load it and my code: 我正在构建一个依赖于jquery被加载的小部件 - 我正在使用以下内容来加载它和我的代码:

(function () {

    var jqueryVersion = (window.jQuery !== undefined) ? window.jQuery.fn.jquery.charAt(0) + window.jQuery.fn.jquery.charAt(2) : 0;

    var jQuery;
    /******** Called once jQuery has loaded ******/
    function scriptLoadHandler() {
        jQuery = window.jQuery.noConflict(true);
        main();
    }

    /******** Load jQuery if not present *********/
    if (parseInt(jqueryVersion) < 17) {
        var script_tag = document.createElement('script');
        script_tag.setAttribute("type", "text/javascript");
        script_tag.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");
        script_tag.onload = scriptLoadHandler;
        script_tag.onreadystatechange = function () { // Same thing but for IE
            if (this.readyState === 'complete' || this.readyState === 'loaded') { scriptLoadHandler(); }
        };
        document.getElementsByTagName("head")[0].appendChild(script_tag);
    } else {
        jQuery = window.jQuery;
        main();
    }

    function main() {
        //my code goes in here
        //leaving blank for now because I still get error
    }

})();

I got most of that from here: http://alexmarandon.com/articles/web_widget_jquery/ 我从这里得到了大部分内容: http//alexmarandon.com/articles/web_widget_jquery/

I need jquery 1.7 or higher because I use the .on() method. 我需要jquery 1.7或更高版本因为我使用.on()方法。 When I run this on a page using jquery that is older than 1.7 I sometimes get errors in IE that lead me to believe there is a conflict with either the old version of jquery or some other js on the page. 当我使用早于1.7的jquery在页面上运行它时,我有时会在IE中遇到错误,导致我认为与旧版本的jquery或页面上的其他js存在冲突。 Some errors: 一些错误:

SCRIPT5007: Unable to get property 'forceInt' of undefined or null reference SCRIPT5007:无法获取未定义或空引用的属性'forceInt'

SCRIPT5007: Object expected SCRIPT5007:预期的对象

SCRIPT5007: Unable to get property 'easing' of undefined or null reference SCRIPT5007:无法获取未定义或空引用的属性“缓动”

SCRIPT438: Object doesn't support property or method 'on' SCRIPT438:对象不支持属性或方法'on'

These errors go away if I change 如果我改变,这些错误就会消失

jQuery = window.jQuery.noConflict(true);

to

jQuery = window.jQuery.noConflict();

Am I doing something wrong? 难道我做错了什么?

Your problem might be caused by the fact that the 'handler' is called twice. 您的问题可能是由于“处理程序”被调用两次这一事实引起的。

http://msdn.microsoft.com/en-us/library/ie/hh180173(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/ie/hh180173(v=vs.85).aspx

Also, taken from http://api.jquery.com/jQuery.noConflict/ 另外,取自http://api.jquery.com/jQuery.noConflict/

"If necessary, you can free up the jQuery name as well by passing true as an argument to the method. This is rarely necessary, and if you must do this (for example, if you need to use multiple versions of the jQuery library on the same page), you need to consider that most plug-ins rely on the presence of the jQuery variable and may not operate correctly in this situation." “如果有必要,你可以通过将true作为参数传递给方法来释放jQuery名称。这很少是必要的,如果你必须这样做(例如,如果你需要使用多个版本的jQuery库)在同一页面),您需要考虑大多数插件依赖于jQuery变量的存在,并且在这种情况下可能无法正常运行。“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM