通过 guid 删除事件处理程序

Remove event handler by guid

我可以获得有关此类元素的所有事件处理程序的信息。

$._data($('#element_id')[0], "events");

这为我提供了每个事件处理程序的记录。

0: {type: "change", origType: "change", data: null, guid: 315, handler: ƒ, …}

我想使用此 GUID 删除特定的事件处理程序。

我知道另一种方法是使用回调函数的名称或命名空间,但这个问题是关于使用 guid 的。

$.prototype.unbindByGuid = function (guid) {
    $(this).each(function (index, element) {
        var handlers = [];

        $.each($._data(element, "events"), function (key, value) {
            $.each(value, function (index, value) {
                if(value.guid == guid) handlers.push({eventType: key, handler: value.handler});
            });
        });

        handlers.forEach(function (handler) { $(element).unbind(handler.eventType, handler.handler); });
    });
}