jQuery off() method
Example
Remove all
click events on the element:
$
(
"
button
"
)
.
click
(
function
(
)
{
$
(
"
p
"
)
.
off
(
"
click
"
)
;
}
)
;
Try it »
Definition and Usage
The off() method is usually used to remove event handlers added by the on() method.
Since jQuery version 1.7, the off() method is the new replacement for the unbind(), die(), and undelegate() methods. This method brings many conveniences to the API; we recommend using it, as it simplifies the jQuery codebase.
**Note:** To remove a specific event handler, the selector string must match the argument passed to the on() method when the handler was added.
**Tip:** To add an event that runs only once and is then removed, use the one() method.
Syntax
$(
selector
).off(
event,selector,function(eventObj),map
)
| Parameters | Description |
|---|---|
| event | Required. Specifies one or more events or namespaces to remove from the selected elements. |
由空格分隔多个事件值。必须是有效的事件。 |
| selector | Optional. Specifies the selector originally passed to the on() method when the event handler was added. | | function(eventObj) | Optional. Specifies the function to run when the event occurs. | | map | Specifies the event map ({event:function, event:function, ...}), containing one or more events to add to the element and the function to run when the event occurs. |
More examples
Switching from unbind() to off()
How to use off() to achieve the same effect as unbind().
Switching from undelegate() to off()
How to use off() to achieve the same effect as undelegate().
Switching from die() to off()
How to use off() to achieve the same effect as die().
Remove all click event handlers added via on()
How to remove all click event handlers added via the on() method to all
elements.
Remove a specified event function added via on()
How to remove a specified function added via the on() method.
Remove an event handler using the event object
How to remove an event handler after the event has been triggered a certain number of times.
jQuery event methods