.undelegate()

.undelegate( ) 官网原E文: jQuery

描述: 为当前选择所匹配的所有元素移除一个事件处理程序,现在或将来,基于一组特定的根元素。

  • version added: 1.4.2.undelegate()

  • version added: 1.4.2.undelegate( selector, eventType )

    selector用于过滤事件结果的选择器。

    eventType一个字符串,其中包含一个JavaScript事件类型,如“点击”或“的keydown”

  • version added: 1.4.2.undelegate( selector, eventType, handler )

    selector用于过滤事件结果的选择器。

    eventType一个字符串,其中包含一个JavaScript事件类型,如“点击”或“的keydown”

    handler一个函数,用来该事件被触发后执行。

  • version added: 1.4.3.undelegate( selector, events )

    selector用于过滤事件结果的选择器。

    events一个或多个事件类型和以前绑定的功能映射到他们解除绑定。

  • version added: 1.6.undelegate( namespace )

    namespace一个字符串,其中包含一个命名空间取消绑定的所有事件。

Undelegate是用来移除使用.delegate()的方式已经绑定的事件处理程序。它的工作原理与.die()用另外一个选择器过滤参数(这是委托工作所需)几乎相同。

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: 解除绑定的所有段落的所有委托点击事件:

$("p").undelegate( "click" )

Example: 为了undelegate只是一个以前绑定的处理程序,通过在作为第三个参数的函数:

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");
jQuery 1.6 API 中文版Clove整理、修订