[英]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.