jquery空函数和事件处理程序

jquery空函数和事件处理程序

本文介绍了jquery空函数和事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在空函数(http://api.jquery.com/empty/)的jquery文档中,有以下语句:

In the jquery documentation of the "empty" function (http://api.jquery.com/empty/) there is the following statement:

"To avoid memory leaks, jQuery removes other constructs such as data and event handlers
from the child elements before removing the elements themselves."

文字说:... ... jQuery从!CHILD!元素中移除事件处理程序...... 。但我希望事件处理程序也从div标签中删除($(#mydiv)。empty)。我知道有删除功能,但我的意图是不删除div标签。完成这项工作的最佳方法是什么?

The text says: "... jQuery removes event handlers from the !C H I L D! elements ...". But I want the event handlers also removed from the div tag ($("#mydiv").empty). I know that there is the function "remove", but my intention is to not remove the div tag. What is the best way to get this done?

另一件事是:

当他们说删除事件处理程序时。他们只删除用bind制作的构造,还是删除用delegate制作的构造?

The other thing is:
When they say "remove event handlers". Do they only remove constructs made with "bind" or do they also remove constructs made with "delegate"?

非常感谢

推荐答案

要从元素中删除所有绑定事件处理程序,可以将特殊值*传递给方法:

To remove all bound event handlers from an element, you can pass the special value "*" to the off() method:

$("#mydiv").empty().off("*");

当文档说删除事件处理程序时,它只说绑定的事件处理程序,而不是委托的事件处理程序,因为它们绑定到一个不受删除影响的祖先元素(或文档本身)。

When the documentation says remove events handlers, it only speaks of bound event handlers, not delegated ones, since these are bound to an ancestor element (or the document itself) which is not impacted by the removal.

如果删除的元素稍后恢复,则允许委派的处理程序按预期工作。

This allows delegated handlers to keep working as intended if the removed element is reinstated later.

这篇关于jquery空函数和事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:58