Attaching jQuery event handlers so that they are triggered first

前端 未结 9 1826
太阳男子
太阳男子 2020-12-05 09:36

Is there a way to attach a jQuery event handler such that the handler is triggered before any previously-attached event handlers? I came across this article, but the code d

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 10:22

    In addition to the selected answer, consider it's missing parameters:

    jQuery.fn.bindUp = function (type, parameters, fn) {
        type = type.split(/\s+/);
    
        this.each(function () {
            var len = type.length;
            while (len--) {
                if (typeof parameters === "function")
                    jQuery(this).bind(type[len], parameters);
                else
                    jQuery(this).bind(type[len], parameters, fn);
    
                var evt = jQuery._data(this, 'events')[type[len]];
                evt.splice(0, 0, evt.pop());
            }
        });
    };
    

提交回复
热议问题