在 jQuery 中,如果删除一个元素,该元素上的任何事件都会被删除吗?

发布于 2024-08-06 14:37:33 字数 212 浏览 2 评论 0原文

例如,如果我有一个链接,其中绑定了以下事件:

$("a.d").bind("click", this, onDelete);

然后执行:

$("a.d").remove();

可以吗?或者它会导致内存泄漏,我需要先调用 unbind 吗?

感谢您的任何帮助。

For example if I have a link with the following event bound to it:

$("a.d").bind("click", this, onDelete);

And later do:

$("a.d").remove();

Is that fine? Or does it cause a memory leak and I need to call unbind 1st?

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

分分钟 2024-08-13 14:37:33

来自 jQuery 文档中的remove()

从列表中删除所有匹配的元素
DOM。这不会将它们从
jQuery 对象,允许您使用
进一步匹配元素。笔记
该函数从1.2.2开始
还将删除所有事件处理程序
和内部缓存的数据。

From jQuery docs for remove()

Removes all matched elements from the
DOM. This does NOT remove them from
the jQuery object, allowing you to use
the matched elements further. Note
that this function starting with 1.2.2
will also remove all event handlers
and internally cached data.

平定天下 2024-08-13 14:37:33

我还没有测试过它,但我相信删除元素将解除其事件处理程序的绑定。我从 jQuery API 文档(搜索删除)得出这个结论,其中指出如果您想移动从 DOM 的一个部分到另一部分的元素:

$("#foo").remove().appendTo("#bar");

应编写以

$("#foo").appendTo("#bar");

避免丢失事件处理程序。

I haven't tested it, but I believe that removing an element will unbind its event handlers. I come to this conclusion from the jQuery API documentation (search for remove) which states that if you want to move an element from one part of the DOM to another that:

$("#foo").remove().appendTo("#bar");

should be written as

$("#foo").appendTo("#bar");

to avoid losing the event handlers.

Bonjour°[大白 2024-08-13 14:37:33

答案是肯定的,只要该事件是用 jQuery 附加的。如果附加诸如“onclick”之类的东西,我不相信它会。

本文讨论了其中的一些内容。它还定义了一个递归函数来删除元素及其所有子元素的所有单击事件。它将涵盖 jQuery 单击处理程序以及使用 onclick 定义的处理程序,以便您了解。

http://www. Computerhowtoguy.com/how-to-use-the-jquery-unbind-method-on-all-child-elements/

function RecursiveUnbind($jElement) {
    // remove this element's and all of its children's click events
    $jElement.unbind();
    $jElement.removeAttr('onclick');
    $jElement.children().each(function () {
        RecursiveUnbind($(this));
    });
}

要使用前面示例中的函数,我们将调用该函数并将其传递给“容器”div作为 jQuery 对象。

RecursiveUnbind($('#container'));

The answer is yes, so long as the event was attached WITH jQuery. If attached with something like "onclick" I don't believe it will.

This article discusses some of that. It also defines a recursive function to remove all click events for an element and all of its children. It will cover jQuery click handlers as well as handlers defined with onclick so you're covered.

http://www.computerhowtoguy.com/how-to-use-the-jquery-unbind-method-on-all-child-elements/

function RecursiveUnbind($jElement) {
    // remove this element's and all of its children's click events
    $jElement.unbind();
    $jElement.removeAttr('onclick');
    $jElement.children().each(function () {
        RecursiveUnbind($(this));
    });
}

To use the function in the previous example we would call the function passing it the “container” div as a jQuery object.

RecursiveUnbind($('#container'));
从来不烧饼 2024-08-13 14:37:33

根据记录,您不必担心 javascript 中的内存泄漏。 (冷静点,不是 C++!)

浏览器的 javascript 引擎管理所有对象,并且垃圾收集它们。当我说对象时,这也意味着事件处理函数,因为函数也是 javascript 中的对象。

无关:我喜欢 javascript 中的一切都是对象:D

干杯!
杰瑞赫

For the record, you need not worry about memory leaks in javascript. (chill man, not c++!)

The browser's javascript engine manages all objects, and garbage collects them. When I say objects, that means event-handling-functions too, because functions are also objects in javascript.

Unrelated: I love how everything is an object in javascript :D

Cheers!
jrh

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文