问题描述
我有两个函数挂钩在表单的 submit
事件中。每个函数都在不同的位置,一个函数可能会影响另一个函数。
I have two functions hooked on the submit
event of a form. Each function is in a different place, and one function can affect the other.
如何强制其中一个函数挂接最低优先级(即:最后一个被执行)?
How can I force one of these functions to be hooked with the lowest priority (ie. be the last to be executed)?
推荐答案
有四种方式可以想到:
-
自己管理回调,只有一个事件处理程序按所需的顺序调用函数。
Manage the callbacks yourself, and only have one event handler that calls the functions in the desired order.
在应该最后触发的函数中,在零毫秒超时中进行实际工作。如果所有其他功能同步工作(并且您可以在活动冒泡时不会发生最后一个功能),这将达到同样的目的。
In the function that is supposed to fired last, do the actual work in a zero-millisecond timeout. If all other functions work synchronously (and you can live with the final one not happening during the event bubbling), this will achieve the same thing.
待处理的最后一个处理程序在DOM树中更高。您必须测试是否与提交
事件一起使用;这些事件的冒泡在IE中无效,但提到这已经在jQuery中进行了规范化。可能值得一试。
Bind the to-be-fired-last handler higher up in the DOM tree. You'll have to test if that works with submit
events; bubbling of these events doesn't work in IE, but the jQuery docs mention that this has been normalized in jQuery. May be worth a try.
有点类似于1.,当绑定任何其他处理程序而不是低优先级处理程序时,解除绑定后者,绑定新的,最后重新绑定最后一次运行。
Somewhat similar to 1., when binding any other handler than the low-priority one, unbind the latter, bind the new one, and finally re-bind the last-to-be-run.
这篇关于jquery事件优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!