.one( events [, data ], handler(eventObject) ) Returns: jQuery
Description: Attach a handler to an event for the elements. The handler is executed at most once per element.
-
version added: 1.1.one( events [, data ], handler(eventObject) )
-
eventsType: StringA string containing one or more JavaScript event types, such as "click" or "submit," or custom event names.
-
dataType: PlainObjectAn object containing data that will be passed to the event handler.
-
handler(eventObject)Type: Function()A function to execute at the time the event is triggered.
-
-
version added: 1.7.one( events [, selector ] [, data ], handler(eventObject) )
-
eventsType: StringOne or more space-separated event types and optional namespaces, such as "click" or "keydown.myPlugin".
-
selectorType: StringA selector string to filter the descendants of the selected elements that trigger the event. If the selector is
null
or omitted, the event is always triggered when it reaches the selected element. -
dataType: AnythingData to be passed to the handler in
event.data
when an event is triggered. -
handler(eventObject)Type: Function()A function to execute when the event is triggered. The value
false
is also allowed as a shorthand for a function that simply doesreturn false
.
-
-
version added: 1.7.one( events [, selector ] [, data ] )
-
eventsType: PlainObjectAn object in which the string keys represent one or more space-separated event types and optional namespaces, and the values represent a handler function to be called for the event(s).
-
selectorType: StringA selector string to filter the descendants of the selected elements that will call the handler. If the selector is null or omitted, the handler is always called when it reaches the selected element.
-
dataType: AnythingData to be passed to the handler in
event.data
when an event occurs.
-
The first form of this method is identical to .bind()
, except that the handler is unbound after its first invocation. The second two forms, introduced in jQuery 1.7, are identical to .on()
except that the handler is removed after the first time the event occurs at the delegated element, whether the selector matched anything or not. For example:
1
2
3
4
5
6
|
|
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:
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.
Examples:
Example: Tie a one-time click to each div.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
|
Example: To display the text of all paragraphs in an alert box the first time each of them is clicked:
1
2
3
|
|