我正在使用 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/