How to order events bound with jQuery

后端 未结 12 1044
闹比i
闹比i 2020-11-22 13:09

Lets say I have a web app which has a page that may contain 4 script blocks - the script I write may be found in one of those blocks, but I do not know which one, that is ha

12条回答
  •  情歌与酒
    2020-11-22 13:43

    Please note that in the jQuery universe this must be implemented differently as of version 1.8. The following release note is from the jQuery blog:

    .data(“events”): jQuery stores its event-related data in a data object named (wait for it) events on each element. This is an internal data structure so in 1.8 this will be removed from the user data name space so it won’t conflict with items of the same name. jQuery’s event data can still be accessed via jQuery._data(element, "events")

    We do have complete control of the order in which the handlers will execute in the jQuery universe. Ricoo points this out above. Doesn't look like his answer earned him a lot of love, but this technique is very handy. Consider, for example, any time you need to execute your own handler prior to some handler in a library widget, or you need to have the power to cancel the call to the widget's handler conditionally:

    $("button").click(function(e){
        if(bSomeConditional)
           e.stopImmediatePropagation();//Don't execute the widget's handler
    }).each(function () {
        var aClickListeners = $._data(this, "events").click;
        aClickListeners.reverse();
    });
    

提交回复
热议问题