我做了一个很少有场景的游戏。每个场景都有自己的javascript,我希望异步加载它,然后将特定场景文件加载到div中。
这是我的html index.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.js"> </script>
<script type="text/javascript" src="first.js" id="first" ></script>
</head>
<body>
<div id="test" style="width: 800px; height: 400px; background: black">
hello
</div>
<button id="submit">submit</button>
</body>
</html>
以下是两个工作功能。
第一次
$(document).ready(function() {
$('#submit').click(function() {
(function() {
var myscript = document.createElement('script');
myscript.type = 'text/javascript';
myscript.src = ('second.js');
var s = document.getElementById('first');
s.parentNode.insertBefore(myscript, s);
})();
});
});
这是第二个
$( "#test" ).load('div.html');
我希望第二个命令:$( "#test“).load('div.html');在脚本加载成功完成后立即执行。
我怎么能这么做?
发布于 2013-05-03 10:01:40
只需将另一个.load()
放到另一个.load()
函数的回调中即可。
$( "#targetelement" ).load('myajaxpage.php', function(){
//call back
$( "#targetelement" ).load('myajaxpage.html', function(){
//call back
})
});
当我意识到你的问题的标题和你问的是什么不同
我希望第二个命令:$( "#test“).load('div.html');在脚本加载成功完成后立即执行。
您需要使用具有callback
功能的脚本加载器,如下所示
head.js("/path/to/jquery.js", "/google/analytics.js", "/js/site.js", function() {
// your function you want to call after the scripts above is loaded
});
yepnope.injectJs("jquery.js", function () {
// your function you want to call after the scripts above is loaded
}, {
charset: "utf-8"
}, 5000);
嗯,有很多脚本加载器,但在我的经验中,上面这两件事对我真的很好
https://stackoverflow.com/questions/16364965
复制