jQuery off()Methods

jQuery 事件方法jQuery Event Methods

Examples

Remove the click event on all <p> elements:

$("button").click(function(){ $("p").off("click"); });

Try it »

Definition and Usage

The off() method is usually used to remove event handlers added viaon()the method.

Since jQuery version 1.7, the off() method is a new replacement for the unbind(), die(), and undelegate() methods. This method brings a lot of convenience to the API. We recommend using this method, as it simplifies the jQuery codebase.

Note:To remove a specific event handler, the selector string must match the parameter passed to the on() method when the event handler was added.

Tip:To add an event that runs only once and then remove it, use theone()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.

Multiple event values are separated by spaces. Must be valid events.
selector Optional. Specifies the selector originally passed to the on() method when adding the event handler.
function(eventObj) Optional. Specifies the function to run when the event occurs.
map Specifies an 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

Change from unbind() to off()
How to use off() to achieve the same effect as unbind().

Change from undelegate() to off()
How to use off() to achieve the same effect as undelegate().

Change 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 to all <p> elements via the on() method.

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 an event has been triggered a certain number of times.


jQuery 事件方法jQuery Event Methods

Other Extensions