如何逐个执行jquery代码? 我想在完成工作时先做“功能1”。然后做“功能2”......
感谢。
<script type="text/javascript">
jQuery(document).ready(function(){
$(document).ready(function(){
//function 2, jqeury.ajax
});
$(document).ready(function(){
//function 3, jqeury.json
});
$('#col').load("home.html");
//function 4, jqeury.load
});
});
</script>
<script type="text/javascript">
jQuery(document).ready(function(){
//function 1, a jquery slider plungin
});
</script>
答案 0 :(得分:9)
您不需要这么多文档就绪电话。一个就足够了
如果你的意思是想要在收到来自你正在进行的AJAX调用的响应后调用每个方法,请将代码放入回调中;
$(document).ready(function(){
one(); //call the initial method
});
function one(){
$.get('url', {}, function(data){
two(); //call two() on callback
})
}
function two(){
$.getJSON('url', {}, function(data){
three(); //ditto
})
}
function three(){
$('#selector').load('url');
}
文档
http://api.jquery.com/jQuery.get/
答案 1 :(得分:2)
使用回调函数,而不是使用多个document.ready()。
<script type="text/javascript">
function ajaxfun() {
//function 2, jqeury.ajax
//in ajax callback call the jsonfun();
}
function jsonfun(){
//function 3, jqeury.json
//after json function add the next function in it callback.
}
</script>
<script type="text/javascript">
jQuery(document).ready(function(){
//function 1, a jquery slider plungin
//Call ajaxfun() here to execute second.
});
</script>
答案 2 :(得分:0)
看起来你正在做三个ajax调用。从jQuery 1.5开始,我们现在从ajax调用返回一个Deferred object(技术上是一个jqXHR对象),所以你可以这样链接它们:
$(function() { // this is a document ready function. it's all you need.
$.ajax(/* your ajax specs */).done(function() {
$.getJSON('somepath').done(function() {
$('#container').load('etc.');
});
});
});
答案 3 :(得分:-1)
使用setTimeout
功能
function f1(para) {
// ...
// do work;
setTimeout(f2, 10);
}
function f2() {
// ...
// do work
setTimeout(f3, 10);
}
答案 4 :(得分:-1)
<script type="text/javascript">
jQuery(document).ready(function(){
function2(){ //declare what function 2 will do here
//function 2, jqeury.ajax
}
function3(){
//function 3, jqeury.json
}
$('#col').load("home.html");
//function 4, jqeury.load
});
});
</script>
<script type="text/javascript">
jQuery(document).ready(function(){
//function 1, a jquery slider plungin
function2(); // execute function 2 after 1
function3();
});
</script>