.undelegate()
.undelegate( ) 返回: jQuery
描述: 為當(dāng)前選擇所匹配的所有元素移除一個(gè)事件處理程序,現(xiàn)在或?qū)?lái),基于一組特定的根元素。
version added: 1.4.2.undelegate()
-
version added: 1.4.2.undelegate( selector, eventType )
selector用于過(guò)濾事件結(jié)果的選擇器。
eventType一個(gè)字符串,其中包含一個(gè)JavaScript事件類型,如“點(diǎn)擊”或“的keydown”
-
version added: 1.4.2.undelegate( selector, eventType, handler )
selector用于過(guò)濾事件結(jié)果的選擇器。
eventType一個(gè)字符串,其中包含一個(gè)JavaScript事件類型,如“點(diǎn)擊”或“的keydown”
handler一個(gè)函數(shù),用來(lái)該事件被觸發(fā)后執(zhí)行。
-
version added: 1.4.3.undelegate( selector, events )
selector用于過(guò)濾事件結(jié)果的選擇器。
events一個(gè)或多個(gè)事件類型和以前綁定的功能映射到他們解除綁定。
-
version added: 1.6.undelegate( namespace )
namespace一個(gè)字符串,其中包含一個(gè)命名空間取消綁定的所有事件。
Undelegate是用來(lái)移除使用.delegate()的方式已經(jīng)綁定的事件處理程序。它的工作原理與.die()用另外一個(gè)選擇器過(guò)濾參數(shù)(這是委托工作所需)幾乎相同。
Examples:
Example: 可以綁定和取消綁定事件的彩色按鈕。
<!DOCTYPE html>
<html>
<head>
<style>
button { margin:5px; }
button#theone { color:red; background:yellow; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.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>
Demo:
Example: 解除綁定的所有段落都從委托的事件:
$("p").undelegate()
Example: 解除綁定的所有段落的所有委托點(diǎn)擊事件:
$("p").undelegate( "click" )
Example: 為了undelegate只是一個(gè)以前綁定的處理程序,通過(guò)在作為第三個(gè)參數(shù)的函數(shù):
var foo = function () {
// code to handle some kind of event
};
$("body").delegate("p", "click", foo); // ... now foo will be called when paragraphs are clicked ...
$("body").undelegate("p", "click", foo); // ... foo will no longer be called.
Example: 為了拆散他們的命名空間的所有委托事件:
var foo = function () {
// code to handle some kind of event
};
// delegate events under the ".whatever" namespace
$("form").delegate("click.whatever", ":button", foo);
$("form").delegate("keypress.whatever", ":text", foo);
// unbind all events delegated under the ".whatever" namespace
$("form").undelegate(".whatever");