How to order events bound with jQuery

后端 未结 12 1043
闹比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:27

    Here's my shot at this, covering different versions of jQuery:

    // Binds a jQuery event to elements at the start of the event chain for that type.
    jQuery.extend({
        _bindEventHandlerAtStart: function ($elements, eventType, handler) {
            var _data;
    
            $elements.bind(eventType, handler);
            // This bound the event, naturally, at the end of the event chain. We
            // need it at the start.
    
            if (typeof jQuery._data === 'function') {
                // Since jQuery 1.8.1, it seems, that the events object isn't
                // available through the public API `.data` method.
                // Using `$._data, where it exists, seems to work.
                _data = true;
            }
    
            $elements.each(function (index, element) {
                var events;
    
                if (_data) {
                    events = jQuery._data(element, 'events')[eventType];
                } else {
                    events = jQuery(element).data('events')[eventType];
                }
    
                events.unshift(events.pop());
    
                if (_data) {
                    jQuery._data(element, 'events')[eventType] = events;
                } else {
                    jQuery(element).data('events')[eventType] = events;
                }
            });
        }
    });
    

提交回复
热议问题