在 jQuery click() 期间处理程序是否总是同步调用?
通过一些简短的测试,似乎 click()
将同步触发任何适用的处理程序(也就是说,这些处理程序都在 click()
之前调用)返回),这对于我正在开发的东西来说是理想的。然而,jQuery 文档似乎并没有保证(没有提到一种或另一种方式)处理程序是同步调用的。
同步行为是否在某处得到保证,或者是否可以安全地假设为跨浏览器且面向未来?
Through some brief testing it appears that click()
will trigger any applicable handlers synchronously (that is, the handlers are all invoked before click()
returns), which is desirable for what I'm developing. However, the jQuery documentation does not seem to guarantee (does not mention one way or the other) that handlers are invoked synchronously.
Is a synchronous behavior guaranteed somewhere, or otherwise safe to assume as cross-browser and future-proof?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,在正常的默认功能启动之前,所有点击事件都会在幕后同步运行。查看此操作的一种方法是将一个或多个点击事件添加到锚标记。如果任何附加函数返回 false。它将取消锚标记的默认功能。换句话说,您不会转到
href
属性中的页面。您会注意到它们同步运行,并且按照它们附加的顺序运行!任何附加函数都可以返回 false 以取消锚标记发生的正常重定向。
当使用 jQuery 触发
click
事件时,所有附加函数将在执行其余链式操作之前运行。因此,上面的代码将始终显示两个警报,然后将链接颜色更改为红色。
Yes, behind the scenes all the click events will run synchronously before the normal default functionality kick in. One way to see this in action is to add one or more
click
events to an anchor tag. If any of the attached functions return false. It will cancel the default functionality of the anchor tag. In other words, you won't go to the page in thehref
attribute.You'll notice that they run synchronously and in the same order they were attached! Any of the attached functions can return false to cancel the normal redirect that happens with anchor tags.
When using jQuery to trigger the
click
event, all the attached functions will run before the rest of the chained actions are performed.So, the code above will ALWAYS show the two alerts then change the link color to red.