gpt4 book ai didi

javascript - 使用 jQuery one() 取消绑定(bind)事件字符串中的所有内容

转载 作者:行者123 更新时间:2023-11-28 19:45:57 25 4
gpt4 key购买 nike

我正在使用 JQuery 的 .one() 函数检查任何交互。

    var myEvents = 'mousedown mousemove mouseup touchstart touchmove touchend mouseout';

jQuery(document).one(myEvents, function(){
//do something only once.
});

但我希望一旦这些事件中的任何一个事件被触发,所有这些都将被绑定(bind)。我知道我可以使用 jQuery(document).unbind(myEvents) 再次取消绑定(bind),但想知道是否有一种干净的内置方法可以在一个事件后简单地取消绑定(bind)。

编辑:这是一个 fiddle :http://jsfiddle.net/4HkQy/6/

最佳答案

您可以使用恰好满足该目的的函数来扩展 jQuery。

jQuery.fn.extend({
oneForAll: function(events, handler) {
var that = this,
oneForAll = function() {
that.unbind(events, oneForAll);
handler.apply(this, arguments);
};

that.on(events, oneForAll);
}
});

您将按照使用one的方式使用它,它只是oneForAll

JSFiddle:Demo

关于javascript - 使用 jQuery one() 取消绑定(bind)事件字符串中的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24286968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com