为什么我得到一个jQuery没有定义的错误? [英] Why am I getting a jQuery is not defined error?
问题描述
document.write('< script language =javascripttype =text / javascriptsrc =http://code.jquery.com/jquery-1.4.4.min.js>< / script>');
$ b $($){
$(document).ready(function(){
alert('it works !!');
});
})(jQuery);
在firefox萤幕控制台中,我看到这个错误:jQuery没有定义,我认为是因为jQuery库是从我的js文件中加载$函数之后的。
你有什么想法可以解决这个问题吗?如果我从firebug控制台运行脚本,一切正常。
你是正确的jquery没有被加载的时间叫它。你将不得不编写一个函数来加载jQuery库,然后再调用你的代码。或者,你可以延迟你的代码执行,但这不是100%完全证明。
更新。
链接检查jQuery是否已经加载有用的回调电话。
I have to use an external js file in order to load jquery. The code from my js file is below:
document.write('<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>');
(function($) {
$(document).ready(function() {
alert('it works!!');
});
})(jQuery);
In the firefox firebug console I see this error: "jQuery is not defined" and I think it is because the jQuery library is loaded after the $ function from my js file.
Do you have any idea about how can I fix this? If I run the script from the firebug console everything works fine.
You are correct about jquery not being loaded at the time you call it. You'll have to write a function that will load the jQuery library and only later call your code. Alternatively you can delay your code execution, but this is not 100% full proof.
Update. Link to check if jQuery has been loaded useful for a callback call.
这篇关于为什么我得到一个jQuery没有定义的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!