什么决定了 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 时
- 隐藏
#foo< 中的
p
元素/code> - 将
p
附加到#bar
- 显示
p
现在是#bar
的子级
实际结果
- 将
p
附加到#bar
- 隐藏
#foo
中的p
元素 - 显示
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
- hide the
p
element in#foo
- append
p
to#bar
- show
p
which is now a child of#bar
Actual Result
- append
p
to#bar
- hide the
p
element in#foo
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
为了确保您在隐藏或显示等效果之后执行某些操作,请使用回调。
http://docs.jquery.com/Effects/show#speedcallback
至添加:
Vincent 是对的,执行实际上是
(缓慢)
您看到的是效果
(已完成)
(完全的)
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
(SLOWLY)
What you saw was the result of the effect
(COMPLETED)
(COMPLETED)
预期结果是正确的。观察到的行为可能是 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.
如果您想等到每个动画完成后再执行下一步,请使用文档:
If you want to wait until each animation completes before doing the next step, use the animation callbacks detailed in the documentation:
show()
和hide()
实际上是动画效果,但是当没有传递参数时,它们使用“即时”持续时间。然而,由于它们是作为动画实现的,这意味着它们不与函数链同步执行。因此,您真正应该使用的是hide()
调用的回调来触发回调函数,该函数调用append()
然后调用show()
。http://docs.jquery.com/Effects/show
http://docs.jquery.com/Effects/hide
show()
andhide()
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 thehide()
call to trigger a callback function which callsappend()
and thenshow()
.http://docs.jquery.com/Effects/show
http://docs.jquery.com/Effects/hide
隐藏是异步的。如果您想等待它完成,则需要将要在其之后运行的所有代码放入您作为参数传递给 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.
很确定它是按照您调用它的顺序执行的,它可能会启动隐藏部分,然后一瞬间它会附加到另一个元素,但动画部分已经开始,它需要超过一毫秒的时间,因为您将其设置为“慢”它的不透明度从 1 跳到 0,例如从 1 到 0.9 再到 0.8(毫秒)。
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.
如果您想在显示隐藏之后运行其他方法(如“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.