同一元素和事件中的多个函数

时间:2022-03-07 09:36:30

What exactly happens if I add several functions in one element to the same event?

如果我将一个元素中的多个函数添加到同一个事件中会发生什么?

By example:

jQuery('#searchButton').click(function(){ /* logic 1 */ );
jQuery('#searchButton').click(function(){ /* logic 2 */ );
  1. always the first function added to an event will be the first to be called?
  2. 总是添加到事件的第一个函数将是第一个被调用的函数?

  3. all browsers support this type of thing?
  4. 所有浏览器都支持这种类型的东西?

  5. functions are called asynchronously?
  6. 函数是异步调用的?

thanks

3 个解决方案

#1


6  

Just to point out something that other answers haven't yet: jQuery indeed guarantees that the events will be triggered in the order they were added (as is referenced in John Hartsock's answer) by way of binding only one event handler to any given element, and holding an internal array of functions bound (which has the additional advantage of being able to remove anonymous functions, that otherwise you wouldn't have a reference to). That then holds true for all browsers.

只是指出其他答案还没有的东西:jQuery确实通过将一个事件处理程序绑定到任何给定元素的方式确保事件将按照它们被添加的顺序触发(在John Hartsock的答案中引用),并持有一个内部函数绑定范围(它具有能够删除匿名函数的额外优势,否则您将无法引用)。这适用于所有浏览器。

However, if you mix this with any other library or use the DOM directly, there are no guarantees. The standard doesn't require implementations to follow any order, so although many implement a FIFO structure internally that isn't guaranteed. Relevant section of the standard.

但是,如果将其与任何其他库混合或直接使用DOM,则无法保证。该标准不要求实现遵循任何顺序,因此尽管许多实现内部的FIFO结构,但不能保证。标准的相关部分。

#2


1  

Always the first function bound to an elements event will be the first to be called?

始终绑定到元素事件的第一个函数将是第一个被调用的函数吗?

Quote from jQuery Docs

从jQuery Docs引用

If there are multiple handlers registered, they will always execute in the order in which they were bound.

如果注册了多个处理程序,它们将始终按照绑定的顺序执行。

Reference

http://api.jquery.com/bind/

#3


1  

They're executed in the order in which they were bound. Here's an excerpt from a jquery quickstart book:

它们按照它们受约束的顺序执行。以下是jquery快速入门书籍的摘录:

http://www.peachpit.com/articles/article.aspx?p=1371947&seqNum=3

#1


6  

Just to point out something that other answers haven't yet: jQuery indeed guarantees that the events will be triggered in the order they were added (as is referenced in John Hartsock's answer) by way of binding only one event handler to any given element, and holding an internal array of functions bound (which has the additional advantage of being able to remove anonymous functions, that otherwise you wouldn't have a reference to). That then holds true for all browsers.

只是指出其他答案还没有的东西:jQuery确实通过将一个事件处理程序绑定到任何给定元素的方式确保事件将按照它们被添加的顺序触发(在John Hartsock的答案中引用),并持有一个内部函数绑定范围(它具有能够删除匿名函数的额外优势,否则您将无法引用)。这适用于所有浏览器。

However, if you mix this with any other library or use the DOM directly, there are no guarantees. The standard doesn't require implementations to follow any order, so although many implement a FIFO structure internally that isn't guaranteed. Relevant section of the standard.

但是,如果将其与任何其他库混合或直接使用DOM,则无法保证。该标准不要求实现遵循任何顺序,因此尽管许多实现内部的FIFO结构,但不能保证。标准的相关部分。

#2


1  

Always the first function bound to an elements event will be the first to be called?

始终绑定到元素事件的第一个函数将是第一个被调用的函数吗?

Quote from jQuery Docs

从jQuery Docs引用

If there are multiple handlers registered, they will always execute in the order in which they were bound.

如果注册了多个处理程序,它们将始终按照绑定的顺序执行。

Reference

http://api.jquery.com/bind/

#3


1  

They're executed in the order in which they were bound. Here's an excerpt from a jquery quickstart book:

它们按照它们受约束的顺序执行。以下是jquery快速入门书籍的摘录:

http://www.peachpit.com/articles/article.aspx?p=1371947&seqNum=3