开发者

jquery: stop everything happening at once

I am just learning jquery, and it took me ages of browsing the jquery api site to create these few lines of code. What I want to do is use ajax to load content from another page and put it into an existing div. currently, the code works but when I click on a link all the steps get executed in one go, so its doing the load() function while the #content div is sliding up...

What I want it to do is to each line step by step, so after one line finishes THEN the next line starts (eg #content div slides up and then the things inside #content get deleted, but currently, #开发者_运维百科content div gets empty before it finishes sliding up)

$(document).ready(function(){

  $("#nav a").click(function(event){
    event.preventDefault();
    $("#content").slideUp();
    $("#content").empty();
    var navlink = $(this).attr("href");
    $("#content").load(navlink + " " + "#content");
    $("#content").slideDown();

  })


});


Use the callback options:

$(document).ready(function(){

  $("#nav a").click(function(event){
    event.preventDefault();
    $("#content").slideUp("normal", function() {
      $("#content").empty()
       .load($(this).attr("href") + " " + "#content")
       .slideDown(); //Just so this won't wrap here, can be one line
    });    
  });
});


most of the jquery functions accept callbacks.


you need make use of the callback of ajax methods in jquery. in particular the callback method which executes when the page you are loading is finished or the onsuccess method which executes when the page you are loading displayed without errors. this ensurs that the sliding up will only occure after the page has loaded

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜