您不能添加脚本的身体,让他们执行。后期加载脚本,必须通过head
元素加载:
(function addJQuery() {
var url = "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js";
var script = document.createElement('script');
script.onload = function() { addJQuery(); }
document.head.appendChild(script);
}());
然而,这无论它本已装载的货物jQuery的,所以这是没有好。这里就是你一般要改为:
(function loadMyCode() {
// do we have jquery? if not, load it.
if (typeof jQuery === "undefined") {
var url = "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js";
var script = document.createElement('script');
script.onload = function() {
// this will run after jquery gets loaded
loadMyCode();
}
document.head.appendChild(script);
return;
}
// if we get here, we know we have jquery
//
// rest of the code goes here.
}());
所有最右边j'之间'和'空间s'不会帮你 – 2013-10-16 23:27:31
对不起,这是我错字。 –