2009-07-05 60 views
19

如何在繼續執行腳本之前等待jQuery函數slideUp()的結束?jQuery - 等到SlideUp()結束()

<script type="text/javascript"> 
$(document).ready(function() { 
    $("div[class=item]").click(function() { 
     var id = $(this).attr("id"); 
     $("#content").slideUp(); 
     switch(id) { 
      // Applications 
      case "rampro": 
       $("#content").css("text-align", "left"); 
       $("#content").load("inc/pages/rampro.html"); 
       $("#content").slideDown(); 
       break 
      case "diskman": 
       $("#content").css("text-align", "left"); 
       $("#content").load("inc/pages/diskman.html"); 
       break 
      case "iconmap": 
       $("#content").css("text-align", "left"); 
       $("#content").load("inc/pages/iconmap.html"); 
       break 
      // Websites 
      case "benoks": 
       $("#content").css("text-align", "left"); 
       $("#content").load("inc/pages/benoks.html"); 
       break 
      case "linkbase": 
       $("#content").css("text-align", "left"); 
       $("#content").load("inc/pages/linkbase.html"); 
       break 
      case "jamesbrooks": 
       $("#content").css("text-align", "left"); 
       $("#content").load("inc/pages/jamesbrooks.html"); 
       break 
      default: 
       $("#content").css("text-align","center"); 
       $("#content").html("<h1>something went wrong, try again!</h1>"); 
     } 
     return false; 
    }); 
}); 

回答

42

使用callback mechanism並在回調你的代碼運行。

$('#something').slideUp('fast', function() { 
    ... stuff to do after the slide is complete... 
});