概述 .undelegate()
返回值:jQuery
描述: 删除当前选择器匹配的所有元素的事件处理程序,根据一组特定根元素的集合。
在jQuery 3.0中,.undelegate()
已被标记为弃用。从jQuery 1.7开始,它已经被.off()方法取代。所以我们不建议使用该方法。
.undelegate
是用来移除使用.delegate()的方式已经绑定的事件处理程序。
示例
可以绑定和取消绑定事件的彩色按钮。
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- button { margin:5px; }
- button#theone { color:red; background:yellow; }
- </style>
- <script src="http://code.jquery.com/jquery-latest.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>
解除绑定的所有段落都从委托的事件:
- $("p").undelegate()
解除绑定的所有段落的所有委托点击事件:
- $("p").undelegate( "click" )
为了undelegate只是一个以前绑定的处理程序,通过在作为第三个参数的函数:
- 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);
为了拆散他们的命名空间的所有委托事件:
- 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");