是什么决定了jQuery链中方法的执行顺序? [英] What determines the execution order of methods in jQuery chains?

查看:138
本文介绍了是什么决定了jQuery链中方法的执行顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML代码

<div id="foo">
    <h1>foo</h1>
    <p>Pellentesque habitant morbi tristique.</p>
</div>
<div id="bar">
    <h1>bar</h1>
</div>

jQuery代码

$('#bar').click(function () {
    $('#foo p').hide('slow').appendTo('#bar').show('slow');
})

预期结果

点击#bar时


  1. 隐藏 p 元素 #foo

  2. 追加 p #bar

  3. 显示 p 这是现在是的孩子#bar

  1. hide the p element in #foo
  2. append p to #bar
  3. show p which is now a child of #bar

实际结果


  1. p 附加到 #bar

  2. 隐藏 #foo 中的 p 元素

  3. 显示 p 现在是的孩子#bar

  1. append p to #bar
  2. hide the p element in #foo
  3. show p which is now a child of #bar

问题


  • 什么决定jQuery链中方法的执行顺序?

  • 我如何确保eac h事件在下次启动之前完成?

推荐答案

如果你想等到每个动画完成在执行下一步之前,请使用文档中详细介绍的动画回调:

If you want to wait until each animation completes before doing the next step, use the animation callbacks detailed in the documentation:

$('#bar').click(function () {
  $('#foo p').hide('slow', function(){
    $(this).appendTo('#bar').show('slow');
  });
});

这篇关于是什么决定了jQuery链中方法的执行顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆