返回值:jQueryundelegate()

移除所有匹配元素上的事件,基于一个指定的根元素的子集。

.undelegate() 方法用于解除通过 .delegate() 绑定的事件。 从 jQuery 1.7 开始,在元素上进行事件绑定和解除绑定的首选方法是 .on() .off()

示例:

为带颜色的按钮绑定和解除事件绑定。

<!DOCTYPE html>
<html>
<head>
<style>
button { margin:5px; }
button#theone { color:red; background:yellow; }
</style>
<script src="jquery.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>

演示:

示例:

可以像下面这样,解除绑定在段落上的所有事件:

jQuery 代码:
$("p").undelegate()

示例:

可以像下面这样,解除绑定在段落上的 click 事件:

jQuery 代码:
$("p").undelegate( "click" )

示例:

通过传入的第三个参数,仅解除绑定先前绑定的事件:

jQuery 代码:
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); 

示例:

通过命名空间解除所有绑定的事件:

jQuery 代码:
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");