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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
<!DOCTYPE html>
<html lang='en'><head><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
<div class="entry-content">
<div class="entry-title roundTop">
<h1 class="jq-clearfix">.undelegate()</h1>
<div class="entry-meta jq-clearfix">
Categories:
<span class="category"><a href="http://api.jquery.com/category/events/" title="View all posts in Events">Events</a> > <a href="http://api.jquery.com/category/events/event-handler-attachment/" title="View all posts in Event Handler Attachment">Event Handler Attachment</a></span>
</div>
</div>
<div id="undelegate1" class="entry method">
<h2 class="jq-clearfix roundTop section-title">
<span class="name">.undelegate( )</span> <span class="returns">Returns: <a class="return" href="http://api.jquery.com/Types/#jQuery">jQuery</a></span>
</h2>
<div class="jq-box roundBottom entry-details">
<p class="desc"><strong>Description: </strong>Remove a handler from the event for all elements which match the current selector, based upon a specific set of root elements.</p>
<ul class="signatures">
<li class="signature" id="undelegate"><h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.4.2/">1.4.2</a></span>.undelegate()</h4></li>
<li class="signature" id="undelegate-selector-eventType">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.4.2/">1.4.2</a></span>.undelegate( selector, eventType )</h4>
<p class="arguement"><strong>selector</strong>A selector which will be used to filter the event results.</p>
<p class="arguement"><strong>eventType</strong>A string containing a JavaScript event type, such as "click" or "keydown"</p>
</li>
<li class="signature" id="undelegate-selector-eventType-handler">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.4.2/">1.4.2</a></span>.undelegate( selector, eventType, handler )</h4>
<p class="arguement"><strong>selector</strong>A selector which will be used to filter the event results.</p>
<p class="arguement"><strong>eventType</strong>A string containing a JavaScript event type, such as "click" or "keydown"</p>
<p class="arguement"><strong>handler</strong>A function to execute at the time the event is triggered.</p>
</li>
<li class="signature" id="undelegate-selector-events">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.4.3/">1.4.3</a></span>.undelegate( selector, events )</h4>
<p class="arguement"><strong>selector</strong>A selector which will be used to filter the event results.</p>
<p class="arguement"><strong>events</strong>A map of one or more event types and previously bound functions to unbind from them.</p>
</li>
<li class="signature" id="undelegate-namespace">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.6/">1.6</a></span>.undelegate( namespace )</h4>
<p class="arguement"><strong>namespace</strong>A string containing a namespace to unbind all events from.</p>
</li>
</ul>
<div class="longdesc"><p>The <code>.undelegate()</code> method is a way of removing event handlers that have been bound using <a href="/delegate"><code>.delegate()</code></a>. <strong>As of jQuery 1.7</strong>, the <a href="http://api.jquery.com/on"><code>.on()</code></a> and <a href="http://api.jquery.com/off"><code>.off()</code></a> methods are preferred for attaching and removing event handlers.</p></div>
<h3>Examples:</h3>
<div class="entry-examples" id="entry-examples">
<div id="example-0">
<h4>Example: <span class="desc">Can bind and unbind events to the colored button.</span>
</h4>
<pre class="prettyprint"><code class="example demo-code"><!DOCTYPE html>
<html>
<head>
<style>
button { margin:5px; }
button#theone { color:red; background:yellow; }
</style>
<script src="http://code.jquery.com/jquery-1.7rc2.js"></script>
</head>
<body>
<button id="theone">Does nothing...</button>
<button id="bind">Bind Click</button>
<button id="unbind">Unbind Click</button>
<div style="display:none;">Click!</div>
<script>
function aClick() {
$("div").show().fadeOut("slow");
}
$("#bind").click(function () {
$("body").delegate("#theone", "click", aClick)
.find("#theone").text("Can Click!");
});
$("#unbind").click(function () {
$("body").undelegate("#theone", "click", aClick)
.find("#theone").text("Does nothing...");
});
</script>
</body>
</html></code></pre>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div>
<div id="example-1">
<h4>Example: <span class="desc">To unbind all delegated events from all paragraphs, write:</span>
</h4>
<pre class="prettyprint"><code class="example">$("p").undelegate()</code></pre>
</div>
<div id="example-2">
<h4>Example: <span class="desc">To unbind all delegated click events from all paragraphs, write:</span>
</h4>
<pre class="prettyprint"><code class="example">$("p").undelegate( "click" )</code></pre>
</div>
<div id="example-3">
<h4>Example: <span class="desc">To undelegate just one previously bound handler, pass the function in as the third argument:</span>
</h4>
<pre class="prettyprint"><code class="example">var foo = function () {
// code to handle some kind of event
};
// ... now foo will be called when paragraphs are clicked ...
$("body").delegate("p", "click", foo);
// ... foo will no longer be called.
$("body").undelegate("p", "click", foo); </code></pre>
</div>
<div id="example-4">
<h4>Example: <span class="desc">To unbind all delegated events by their namespace:</span>
</h4>
<pre class="prettyprint"><code class="example">var foo = function () {
// code to handle some kind of event
};
// delegate events under the ".whatever" namespace
$("form").delegate(":button", "click.whatever", foo);
$("form").delegate("input[type='text']", "keypress.whatever", foo);
// unbind all events delegated under the ".whatever" namespace
$("form").undelegate(".whatever");</code></pre>
</div>
</div>
</div>
</div>
</div>
</body></html>
|