|
24 | 24 | </argument>
|
25 | 25 | </signature>
|
26 | 26 | <longdesc>
|
27 |
| - <p>The <code>off()</code> method removes event handlers that were attached with <a href="http://api.jquery.com/on"><code>.on()</code></a>. See the discussion of delegated and directly bound events on that page for more information. Specific event handlers can be removed on elements by providing combinations of event names, namespaces, selectors, or handler function names. <strong>When multiple filtering arguments are given, all of the arguments provided must match for the event handler to be removed.</strong></p> |
| 27 | + <p>The <code>off()</code> method removes event handlers that were attached with <a href="/on/"><code>.on()</code></a>. See the discussion of delegated and directly bound events on that page for more information. Specific event handlers can be removed on elements by providing combinations of event names, namespaces, selectors, or handler function names. <strong>When multiple filtering arguments are given, all of the arguments provided must match for the event handler to be removed.</strong></p> |
28 | 28 | <p>If a simple event name such as <code>"click"</code> is provided, <em>all</em> events of that type (both direct and delegated) are removed from the elements in the jQuery set. When writing code that will be used as a plugin, or simply when working with a large code base, best practice is to attach and remove events using namespaces so that the code will not inadvertently remove event handlers attached by other code. All events of all types in a specific namespace can be removed from an element by providing just a namespace, such as <code>".myPlugin"</code>. At minimum, either a namespace or event name must be provided.</p>
|
29 | 29 | <p>To remove specific delegated event handlers, provide a <code>selector</code> argument. The selector string must exactly match the one passed to <code>.on()</code> when the event handler was attached. To remove all delegated events from an element without removing non-delegated events, use the special value <code>"**"</code>.</p>
|
30 |
| - <p>A handler can also be removed by specifying the function name in the <code>handler</code> argument. When jQuery attaches an event handler, it assigns a unique id to the handler function. Handlers proxied by <a href="http://api.jquery.com/jQuery.proxy"><code>jQuery.proxy()</code></a> or a similar mechanism will all have the same unique id (the proxy function), so passing proxied handlers to <code>.off</code> may remove more handlers than intended. In those situations it is better to attach and remove event handlers using namespaces.</p> |
| 30 | + <p>A handler can also be removed by specifying the function name in the <code>handler</code> argument. When jQuery attaches an event handler, it assigns a unique id to the handler function. Handlers proxied by <a href="/jQuery.proxy/"><code>jQuery.proxy()</code></a> or a similar mechanism will all have the same unique id (the proxy function), so passing proxied handlers to <code>.off</code> may remove more handlers than intended. In those situations it is better to attach and remove event handlers using namespaces.</p> |
31 | 31 | <p>As with <code>.on()</code>, you can pass <code>events</code> as an object instead of specifying an <code>events</code> string and <code>handler</code> function as separate arguments. The keys for the <code>events</code> object are events and/or namespaces; the values are handler functions or the special value <code>false</code>.</p>
|
32 | 32 | </longdesc>
|
33 | 33 | <example>
|
34 | 34 | <desc>Add and remove event handlers on the colored button.</desc>
|
35 | 35 | <code><![CDATA[
|
36 | 36 | function aClick() {
|
37 |
| - $("div").show().fadeOut("slow"); |
| 37 | + $( "div" ).show().fadeOut( "slow" ); |
38 | 38 | }
|
39 |
| -$("#bind").click(function () { |
40 |
| - $("body").on("click", "#theone", aClick) |
41 |
| - .find("#theone").text("Can Click!"); |
| 39 | +$( "#bind" ).click(function() { |
| 40 | + $( "body" ).on( "click", "#theone", aClick ) |
| 41 | + .find( "#theone" ) |
| 42 | + .text( "Can Click!" ); |
42 | 43 | });
|
43 |
| -$("#unbind").click(function () { |
44 |
| - $("body").off("click", "#theone", aClick) |
45 |
| - .find("#theone").text("Does nothing..."); |
| 44 | +$( "#unbind" ).click(function() { |
| 45 | + $( "body" ).off( "click", "#theone", aClick ) |
| 46 | + .find( "#theone" ) |
| 47 | + .text( "Does nothing..." ); |
46 | 48 | });
|
47 | 49 | ]]></code>
|
48 | 50 | <css><![CDATA[
|
49 |
| -button { margin:5px; } |
50 |
| -button#theone { color:red; background:yellow; } |
| 51 | + button { |
| 52 | + margin: 5px; |
| 53 | + } |
| 54 | + button#theone { |
| 55 | + color: red; |
| 56 | + background: yellow; |
| 57 | + } |
51 | 58 | ]]></css>
|
52 |
| - <html><![CDATA[<button id="theone">Does nothing...</button> |
| 59 | + <html><![CDATA[ |
| 60 | +<button id="theone">Does nothing...</button> |
53 | 61 | <button id="bind">Add Click</button>
|
54 | 62 | <button id="unbind">Remove Click</button>
|
55 |
| -<div style="display:none;">Click!</div>]]></html> |
| 63 | +<div style="display:none;">Click!</div> |
| 64 | +]]></html> |
56 | 65 | </example>
|
57 | 66 | <example>
|
58 | 67 | <desc>Remove all event handlers from all paragraphs:</desc>
|
59 |
| - <code><![CDATA[$("p").off()]]></code> |
| 68 | + <code><![CDATA[ |
| 69 | +$( "p" ).off(); |
| 70 | +]]></code> |
60 | 71 | </example>
|
61 | 72 | <example>
|
62 | 73 | <desc>Remove all delegated click handlers from all paragraphs:</desc>
|
63 |
| - <code><![CDATA[$("p").off( "click", "**" )]]></code> |
| 74 | + <code><![CDATA[ |
| 75 | +$( "p" ).off( "click", "**" ); |
| 76 | +]]></code> |
64 | 77 | </example>
|
65 | 78 | <example>
|
66 | 79 | <desc>Remove just one previously bound handler by passing it as the third argument:</desc>
|
67 |
| - <code><![CDATA[var foo = function () { |
68 |
| - // code to handle some kind of event |
| 80 | + <code><![CDATA[ |
| 81 | +var foo = function() { |
| 82 | + // Code to handle some kind of event |
69 | 83 | };
|
70 | 84 |
|
71 |
| -// ... now foo will be called when paragraphs are clicked ... |
72 |
| -$("body").on("click", "p", foo); |
| 85 | +// ... Now foo will be called when paragraphs are clicked ... |
| 86 | +$( "body" ).on( "click", "p", foo ); |
73 | 87 |
|
74 |
| -// ... foo will no longer be called. |
75 |
| -$("body").off("click", "p", foo); ]]></code> |
| 88 | +// ... Foo will no longer be called. |
| 89 | +$( "body" ).off( "click", "p", foo ); |
| 90 | +]]></code> |
76 | 91 | </example>
|
77 | 92 | <example>
|
78 | 93 | <desc>Unbind all delegated event handlers by their namespace:</desc>
|
79 |
| - <code><![CDATA[var validate = function () { |
80 |
| - // code to validate form entries |
| 94 | + <code><![CDATA[ |
| 95 | +var validate = function() { |
| 96 | + // Code to validate form entries |
81 | 97 | };
|
82 | 98 |
|
83 |
| -// delegate events under the ".validator" namespace |
84 |
| -$("form").on("click.validator", "button", validate); |
| 99 | +// Delegate events under the ".validator" namespace |
| 100 | +$( "form" ).on( "click.validator", "button", validate ); |
85 | 101 |
|
86 |
| -$("form").on("keypress.validator", "input[type='text']", validate); |
| 102 | +$( "form" ).on( "keypress.validator", "input[type='text']", validate ); |
87 | 103 |
|
88 |
| -// remove event handlers in the ".validator" namespace |
89 |
| -
|
90 |
| -$("form").off(".validator");]]></code> |
| 104 | +// Remove event handlers in the ".validator" namespace |
| 105 | +$( "form" ).off( ".validator" ); |
| 106 | +]]></code> |
91 | 107 | </example>
|
92 | 108 | <category slug="events/event-handler-attachment"/>
|
93 | 109 | <category slug="version/1.7"/>
|
|
0 commit comments