jQuery bind event listener before another

后端 未结 6 1038
無奈伤痛
無奈伤痛 2020-12-13 21:00

Using the prettyPhoto plugin to open modal-style content containers, and trying to integrate with Google Analytics\' Event Tracking to track when videos get opened.

6条回答
  •  借酒劲吻你
    2020-12-13 21:47

    Here is a variant of the solution using the more modern .On() approach.

    // Same as .on() but moves the binding to the front of the queue.
    $.fn.priorityOn = function (type, selector, data, fn) {
        this.each(function () {
            var $this = $(this);
    
            var types = type.split(" ");
    
            for (var t in types) {
                $this.on(types[t], selector, data, fn);
    
                var currentBindings = $._data(this, 'events')[types[t]];
                if ($.isArray(currentBindings)) {
                    currentBindings.unshift(currentBindings.pop());
                }
            }
    
    
        });
        return this;
    };
    

    Usage is like

    $(document).priorityOn("click blur change", ".some .selector input", function (e) {
        // Your code.
    });
    

提交回复
热议问题