原文: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
|
|
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
|
|
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()
和该方法的作用是一样的。
如果该方法的第一个参数包含多个用空格分隔的事件类型的话,那么每种类型的事件被触发时,处理函数仅会被每个事件类型调用一次。