附着jQuery的事件处理程序,以便它们的第一触发 [英] Attaching jQuery event handlers so that they are triggered first

查看:141
本文介绍了附着jQuery的事件处理程序,以便它们的第一触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法附加一个jQuery的事件处理程序,因此任何previously附加事件处理程序之前处理程序被触发?我碰到这篇文章,但code没有因为工作事件处理程序没有再存储在数组中,这是他的code所期望的。我试图创建一个jQuery的扩展做我想要的东西,但是这不工作(该事件仍然在激发他们被绑定的顺序排列):

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 didn't work because event handlers are no-longer stored in an array, which is what his code expected. I attempted to create a jQuery extension to do what I wanted, but this is not working (the events still fire in the order they were bound):

$.fn.extend({
    bindFirst: function(type, handler) {

        var baseType = type;
        var dotIdx = type.indexOf('.');
        if (dotIdx >= 0) {
            baseType = type.substr(0, dotIdx);
        }

        this.each(function() {
            var oldEvts = {};
            var data = $.data(this);
            var events = data.events || data.__events__;
            var handlers = events[baseType];
            for (var h in handlers) {
                if (handlers.hasOwnProperty(h)) {
                    oldEvts[h] = handlers[h];
                    delete handlers[h];
                    // Also tried an unbind here, to no avail
                }
            }

            var self = $(this);
            self.bind(type, handler);

            for (var h in oldEvts) {
                if (oldEvts.hasOwnProperty(h)) {
                    self.bind(baseType, oldEvts[h]);
                }
            }
        });
    }
});

有没有的自然的方式来重新排序事件处理?如果没有,你知道我的技术可以申请吗?我使用jQuery 1.4.1,不过我会,如果我必须升级。

Is there a natural way to reorder event handling? If there isn't, do you know of technique I could apply? I'm using jQuery 1.4.1, though I'll upgrade if I must.

推荐答案

下面是一个简单的插件,我做了一段时间回来。让你的处理程序绑定到列表的开头。这是很简单的,我也不能保证它与命名空间中的事件或任何花哨的可怕

Here's a simple plugin I did a while back. Lets you bind a handler to the beginning of the list. It is very simple, and I wouldn't guarantee that it works with namespaced events or anything terribly fancy.

有关简单绑定事件的单个或空格分隔群,它应该工作。

For simply binding a single or space separate group of events, it should work.

示例: http://jsfiddle.net/gbcUy/

$.fn.bindUp = function(type, fn) {

    type = type.split(/\s+/);

    this.each(function() {
        var len = type.length;
        while( len-- ) {
            $(this).bind(type[len], fn);

            var evt = $.data(this, 'events')[type[len]];
            evt.splice(0, 0, evt.pop());
        }
    });
};

或者,如果你想操纵处理器阵列中其他一些方式,只得到了你想要的元素的处理,但是并操纵它你想要的:

Or if you wanted to manipulate the Array of handlers in some other manner, just get the handlers for the element you want, and manipulate it however you want:

示例: http://jsfiddle.net/gbcUy/1/

var clickHandlers = $('img').data('events').click;

clickHandlers.reverse(); // reverse the order of the Array

这篇关于附着jQuery的事件处理程序,以便它们的第一触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆