假设我的网页上有一个按钮:
<input type='button' id='myButton' value='Click me!' />
假设我编写以下 jQuery:
$(document).ready(function() {
var button = $('#myButton');
button.click(doThis);
button.click(doThat);
}
function doThis() {
// do something
}
function doThat() {
//do something else
}
当点击按钮时,我如何知道应该首先执行哪个函数?或者这取决于我无法控制的事情?
最佳答案
处理程序按照附加到元素的顺序执行(请参阅 documentation ):
If there are multiple handlers registered, they will always execute in the order in which they were bound.
因此,在您的情况下,首先执行 doThis
,然后执行 doThat
。
注意:这仅适用于 jQuery。有关纯 DOM 事件处理程序的信息,请参阅 @StuperUser's answer .
有关 JavaScript 中事件处理的一般信息,我可以推荐 the articles at quirksmode.org .
关于javascript - 我的元素上的哪个 jQuery 附加事件首先触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6999372/