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

发布于 2024-08-08 14:29:46 字数 927 浏览 6 评论 0原文

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. 隐藏 #foo< 中的 p 元素/code>
  2. p 附加到 #bar
  3. 显示 p 现在是 #bar 的子级

实际结果

  1. p 附加到 #bar
  2. 隐藏 #foo 中的 p 元素
  3. 显示 p 现在是 #bar 的子元素

问题

  • 是什么决定了 jQuery 链中方法的执行顺序?
  • 如何确保每个事件在下一个事件开始之前完成?

HTML Code

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

jQuery Code

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

Expected Result

When #bar is clicked

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

Actual Result

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

Questions

  • What determines the execution order of methods in jQuery chains?
  • How can I ensure that each event completes before the next starts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

街角卖回忆 2024-08-15 14:29:46

为了确保您在隐藏或显示等效果之后执行某些操作,请使用回调。
http://docs.jquery.com/Effects/show#speedcallback

至添加:

Vincent 是对的,执行实际上是

  1. 开始隐藏 #foo 中的 p 元素
    (缓慢)
  2. 将 p 附加到 #bar (快速)
  3. 开始显示 p,它现在是 #bar 的子级 (缓慢)

您看到的是效果

  1. 附加 p 到 #bar (执行)
  2. 隐藏 # 中的 p 元素 的结果富
    (已完成)
  3. 显示 p 现在是#bar 的子级
    (完全的)

To ensure you execute something AFTER an effect like hide or show, use a callback.
http://docs.jquery.com/Effects/show#speedcallback

To Add:

Vincent is right, the execution is actually

  1. start hiding the p element in #foo
    (SLOWLY)
  2. append p to #bar (in a snap)
  3. start showing p which is now a child of #bar (SLOWLY)

What you saw was the result of the effect

  1. append p to #bar (executed)
  2. hide the p element in #foo
    (COMPLETED)
  3. show p which is now a child of #bar
    (COMPLETED)
红焚 2024-08-15 14:29:46

预期结果是正确的。观察到的行为可能是 hide('slow') 异步运行的结果。因此它会在下一个操作执行时运行。所以看起来好像 p 首先附加到 #bar 上。你可以尝试 hide() 而不用放慢速度,看看是否有区别。

The expected result is correct. The observed behaviour may be a result of hide('slow') which runs asynchronously. So it runs while the next action executes. So it appears as if p is appended to #bar first. You can try hide() without the slow to see if that makes a difference.

橘亓 2024-08-15 14:29:46

如果您想等到每个动画完成后再执行下一步,请使用文档:

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

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');
  });
});
蓝眸 2024-08-15 14:29:46

show()hide() 实际上是动画效果,但是当没有传递参数时,它们使用“即时”持续时间。然而,由于它们是作为动画实现的,这意味着它们不与函数链同步执行。因此,您真正应该使用的是 hide() 调用的回调来触发回调函数,该函数调用 append() 然后调用 show()

http://docs.jquery.com/Effects/show

http://docs.jquery.com/Effects/hide

show() and hide() are actually animation effects but when no arguments are passed they use an "instant" duration. However, due to the fact that they're implemented as animations, that means that they don't execute synchronously with the function chain. Thus, what you really should be using is a callback off of the hide() call to trigger a callback function which calls append() and then show().

http://docs.jquery.com/Effects/show

http://docs.jquery.com/Effects/hide

慈悲佛祖 2024-08-15 14:29:46

隐藏是异步的。如果您想等待它完成,则需要将要在其之后运行的所有代码放入您作为参数传递给 hide 的回调函数中。

Hide is asynchronous. If you want to wait for it to finish, you need to put all the code you want to run after it into a callback function that you pass to hide as a parameter.

北笙凉宸 2024-08-15 14:29:46

很确定它是按照您调用它的顺序执行的,它可能会启动隐藏部分,然后一瞬间它会附加到另一个元素,但动画部分已经开始,它需要超过一毫秒的时间,因为您将其设置为“慢”它的不透明度从 1 跳到 0,例如从 1 到 0.9 再到 0.8(毫秒)。

$.fn.hide = function() { alert('hiding'); return this; };
$.fn.appendTo = function() { alert('appending To') }
$('body').hide().appendTo('html')

Pretty sure it's executed in the order you invoke it, it probably starts the hide part and a split second later it's appended to that other element but the animation part has already begun, it takes longer than a millisecond because you set it to 'slow' and it's jumping in opacity from 1 to 0, going from say 1 to .9 to .8 in milliseconds.

$.fn.hide = function() { alert('hiding'); return this; };
$.fn.appendTo = function() { alert('appending To') }
$('body').hide().appendTo('html')
终止放荡 2024-08-15 14:29:46

如果您想在显示隐藏之后运行其他方法(如“Sixten Otto”所说),则必须使用回调。 show hide 的动画不会等待append方法执行。它使用 setInterval 启动一个单独的线程,同时调用其他代码。所以你得到的结果并不出人意料。

You have to use callback if you want to queue the other methods run after the show hide thing like "Sixten Otto" has said. The animation of show hide will not wait for append method to execute. It starts a separate thread with setInterval while in the meantime, other code is invoked. So the result you got is not unexpected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文