简体   繁体   English

无法动态加载jQuery UI-未捕获的TypeError:无法读取未定义的属性'ui'

[英]Unable to load jQuery UI dynamically - Uncaught TypeError: Cannot read property 'ui' of undefined

I load my jQuery and jQuery UI files dynamically. 我动态加载jQuery和jQuery UI文件。 The jQuery file loads successfully but when the jQuery UI file loads an error occurs jQuery文件成功加载,但是当jQuery UI文件加载时发生错误

The following is what is shown in the console : Uncaught TypeError: Cannot read property 'ui' of undefined 以下是控制台中显示的内容:未捕获的TypeError:无法读取未定义的属性'ui'

My code is given below 我的代码如下

(function()
{
var jQuery;
if (window.jQuery === undefined)
{
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type", "text/javascript");
    script_tag.setAttribute("src",
            "//code.jquery.com/jquery-1.11.0.min.js");




    if (script_tag.readyState)
    {
        script_tag.onreadystatechange = function()
        {
            if (this.readyState === 'complete' || this.readyState === 'loaded')
            {
                scriptLoadHandler();
            }
        };
    }

    else
    {
        script_tag.onload = scriptLoadHandler;
    }
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);

}

else
{
    jQuery = window.jQuery;

    main();
}

function scriptLoadHandler()
{
    jQuery = window.jQuery.noConflict(true);

    main();
}

function main() {

    jQuery(document).ready(function($) {
        jQuery.getScript('http://code.jquery.com/ui/1.11.1/jquery-ui.min.js', function() {
            jQuery.noConflict(true);
        });
};
})();

Can someone help me with this? 有人可以帮我弄这个吗?

Simply remove the true from your noConflict call; 只需从noConflict调用中删除true; this relinquishes control over $ but leaves jQuery around for jQuery UI to use: 这放弃了对$的控制,但留下了jQuery供jQuery UI使用:

/******** Called once jQuery has loaded ******/

    function scriptLoadHandler() {
        // Restore $ to its previous values and store the
        // new jQuery in our local jQuery variable
        jQuery = window.jQuery.noConflict(); // no argument!
        // Call our main function
        main();
    }

Use : 采用 :

$(document).ready(function() {}); 

or 要么

$(function() {});

The two statements are actually the exact same. 这两个语句实际上是完全相同的。 So the second call is just a shortcut for the first. 因此,第二个呼叫只是第一个的捷径。

The $ notation is again only a shortcut for jQuery. $表示法再次只是jQuery的快捷方式。 If you have loaded jQuery into your website you can use both. 如果您已经将jQuery加载到您的网站中,则可以同时使用两者。

 (function($){
 }(jQuery));

Here You're calling that anonymous function (which has $ as parameter) and pass the jQuery object in. 在这里,您正在调用该匿名函数(具有$作为参数)并传入jQuery对象。

暂无
暂无

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

相关问题 jQuery UI:未捕获的TypeError:无法读取未定义的属性'nodeType' - jQuery UI : Uncaught TypeError: Cannot read property 'nodeType' of undefined JQuery UI Slider Uncaught TypeError:无法读取未定义的属性“addClass” - JQuery UI Slider Uncaught TypeError: Cannot read property 'addClass' of undefined jQuery UI:未捕获的TypeError:无法读取未定义的属性“display” - jQuery UI: Uncaught TypeError: Cannot read property 'display' of undefined 未捕获(承诺)TypeError:无法读取未定义的属性“ ui5” - Uncaught (in promise) TypeError: Cannot read property 'ui5' of undefined Kendo ui:未捕获的类型错误:无法读取未定义的属性“toLowerCase” - Kendo ui: Uncaught TypeError: Cannot read property 'toLowerCase' of undefined jquery-ui.js:12443未捕获的TypeError:无法读取undefined的属性'apply' - jquery-ui.js:12443 Uncaught TypeError: Cannot read property 'apply' of undefined jquery-ui.js:8056 Uncaught TypeError: Cannot read property 'left' of undefined - jquery-ui.js:8056 Uncaught TypeError: Cannot read property 'left' of undefined AngularJS ui路由器ui-sref导致“未捕获的TypeError:无法读取未定义的属性'0'”错误 - AngularJS ui-router ui-sref causing “Uncaught TypeError: Cannot read property '0' of undefined” error 什么是 Uncaught TypeError:无法读取 AppBar + Drawer 组件(ReactJS + Material-UI)未定义的属性“打开”? - What is Uncaught TypeError: Cannot read property 'open' of undefined for AppBar + Drawer component (ReactJS + Material-UI)? 未捕获的TypeError:无法读取未定义的属性'val'(ui-nestable.min.js) - Uncaught TypeError: Cannot read property 'val' of undefined (ui-nestable.min.js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM