jQuery: more than one handler for same event

后端 未结 8 1237
终归单人心
终归单人心 2020-11-27 03:14

What happens if I bind two event handlers to the same event for the same element?

For example:

var elem = $(\"...\")
elem.click(...);
elem.click(...)         


        
相关标签:
8条回答
  • 2020-11-27 03:23

    You should be able to use chaining to execute the events in sequence, e.g.:

    $('#target')
      .bind('click',function(event) {
        alert('Hello!');
      })
      .bind('click',function(event) {
        alert('Hello again!');
      })
      .bind('click',function(event) {
        alert('Hello yet again!');
      });
    

    I guess the below code is doing the same

    $('#target')
          .click(function(event) {
            alert('Hello!');
          })
          .click(function(event) {
            alert('Hello again!');
          })
          .click(function(event) {
            alert('Hello yet again!');
          });
    

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

    TFM also says:

    When an event reaches an element, all handlers bound to that event type for the element are fired. If there are multiple handlers registered, they will always execute in the order in which they were bound. After all handlers have executed, the event continues along the normal event propagation path.

    0 讨论(0)
  • 2020-11-27 03:33

    jquery will execute both handler since it allows multiple event handlers. I have created sample code. You can try it

    demo

    0 讨论(0)
  • 2020-11-27 03:35

    jQuery's .bind() fires in the order it was bound:

    When an event reaches an element, all handlers bound to that event type for the element are fired. If there are multiple handlers registered, they will always execute in the order in which they were bound. After all handlers have executed, the event continues along the normal event propagation path.

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

    Because jQuery's other functions (ex. .click()) are shortcuts for .bind('click', handler), I would guess that they are also triggered in the order they are bound.

    0 讨论(0)
  • 2020-11-27 03:36

    Both handlers will run, the jQuery event model allows multiple handlers on one element, therefore a later handler does not override an older handler.

    The handlers will execute in the order in which they were bound.

    0 讨论(0)
  • 2020-11-27 03:36

    Made it work successfully using the 2 methods: Stephan202's encapsulation and multiple event listeners. I have 3 search tabs, let's define their input text id's in an Array:

    var ids = new Array("searchtab1", "searchtab2", "searchtab3");
    

    When the content of searchtab1 changes, I want to update searchtab2 and searchtab3. Did it this way for encapsulation:

    for (var i in ids) {
        $("#" + ids[i]).change(function() {
            for (var j in ids) {
                if (this != ids[j]) {
                    $("#" + ids[j]).val($(this).val());
                }
            }
        });
    }
    

    Multiple event listeners:

    for (var i in ids) {
        for (var j in ids) {
            if (ids[i] != ids[j]) {
                $("#" + ids[i]).change(function() {
                    $("#" + ids[j]).val($(this).val());
                });
            }
        }
    }
    

    I like both methods, but the programmer chose encapsulation, however multiple event listeners worked also. We used Chrome to test it.

    0 讨论(0)
  • 2020-11-27 03:40

    There is a workaround to guarantee that one handler happens after another: attach the second handler to a containing element and let the event bubble up. In the handler attached to the container, you can look at event.target and do something if it's the one you're interested in.

    Crude, maybe, but it definitely should work.

    0 讨论(0)
提交回复
热议问题