这是一个非常简单的脚本,应该加载jQuery。我可以在jQuery是加载Firebug的脚本选项卡看到,但我得到“当我尝试使用$没有定义”的错误。谁能帮我明白了什么是错的?在Javascript(js)文件中加载jQuery
//function to add scripts
function include(file)
{
var script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.defer = true;
document.getElementsByTagName('head').item(0).appendChild(script);
}
//add jQuery
include('https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
//See if jQuery is working
$(document).ready(function() {
$('#internal').show();
})
////////////
//RETURNS: "$ is not defined $(document).ready(function() {"
奇怪的是,如果唐“T尝试使用jQuery在这同一个脚本,而不是我加载使用jQuery的另一个js文件它的工作
//function to add scripts
function include(file)
{
var script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.defer = true;
document.getElementsByTagName('head').item(0).appendChild(script);
}
//add jQuery
include('https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
//add my custom script that wants to use jQuery
include('scripts/testScript.js')
testScript.js
$(document).ready(function() {
$('#external').show();
})
我很欣赏这个任何建议。
+1 - 你需要使用该脚本的' onload'处理程序来触发回调。 – user113716 2010-11-19 18:13:19