jquery中的.one()

原文:http://api.jquery.com/one/

参考译文:http://www.css88.com/jqapi-1.9/one/


The .one() method is identical to .on(), except that the handler is unbound after its first invocation. For example:

它和.on()是相同的,除了处理程序在委派的元素上触发事件后第一时间被删除。

这个方法的第一种形式(.one( events [, data ], handler(eventObject) 这种形式))用法和.bind()相同,但.one()的处理函数第一次调用后绑定的事件就被解除了。第二两种形式(.one( events [, selector ] [, data ], handler(eventObject) )这种形式),在jQuery1.7中,它和.on()是相同的,不同之处在于,不管选择器是否匹配到任何元素,或没有匹配任何元素,处理程序在委派的元素上触发事件后第一时间被删除,举个例子:

1
2
3
$( "#foo" ).one( "click", function() {
alert( "This will be displayed only once." );
});

After the code is executed, a click on the element with ID foo will display the alert. Subsequent clicks will do nothing. This code is equivalent to:

在代码执行后,点击id为foo的元素将显示警报。之后再在该元素上点击时,就不会再触发该事件。此代码是等效于:


1
2
3
4
$( "#foo" ).on( "click", function( event ) {
alert( "This will be displayed only once." );
$( this ).off( event );
});

In other words, explicitly calling .off() from within a regularly-bound handler has exactly the same effect.

If the first argument contains more than one space-separated event types, the event handler is called once for each event type.

换句话说,在一个普通的事件绑定处理函数中,显式调用 .off()和该方法的作用是一样的。

如果该方法的第一个参数包含多个用空格分隔的事件类型的话,那么每种类型的事件被触发时,处理函数仅会被每个事件类型调用一次。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值