jQuery - 如何在前一个语句完成后才能执行语句?

Jef*_*eff 0 jquery

例如,我在$ .ajax函数中使用beforeSend选项.在下面的代码示例中,.html函数将在之前的语句中发生淡出时执行.我怎么能阻止这种行为?

 jQuery("#container").fadeOut("slow", function() {
     jQuery("#container").removeClass('error');
 });

 jQuery("#container").html("success").fadeIn("slow");
Run Code Online (Sandbox Code Playgroud)

所以会发生的是,淡出期间,jquery将注入html"成功".我希望它在动画完成后发生.

我该怎么办呢?

谢谢!

Jos*_*ola 5

使用回调...

jQuery("#container").fadeOut("slow", function() {
  // At this point, animation is complete, and the element is invisible
  jQuery(this).removeClass("error").html("success").fadeIn("slow");
});
Run Code Online (Sandbox Code Playgroud)

  • @Josh Stodola - $(this)实际上会引用#container,无需重新查询 (2认同)