jQuery bind event listener before another

后端 未结 6 1050
無奈伤痛
無奈伤痛 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:48

    Worked for me with older and newer jQuery versions:

    /**
     * @link http://stackoverflow.com/a/26892146/655224
     */
    jQuery.fn.getEvents = function() {
        if (typeof(jQuery._data) == 'function') {
            return jQuery._data(this.get(0), 'events') || {};
        } else if (typeof(this.data) == 'function') { // jQuery version < 1.7.?
            return this.data('events') || {};
        }
        return {};
    };
    
    jQuery.fn.preBind = function(type, data, fn) {
        this.each(function () {
            var $this = jQuery(this);
    
            $this.bind(type, data, fn);
    
            var currentBindings = $this.getEvents()[type];
            if (jQuery.isArray(currentBindings)) {
                currentBindings.unshift(currentBindings.pop());
            }
        });
        return this;
    };
    

    But beware, this functions can only return/prebind that events that was set with jQuery itself.

    Special thanks to @jonathanconway and @Patrick...

提交回复
热议问题