在JQuery中分离是否会破坏DOM中的节点? [英] Does detach in JQuery destroys the node in DOM?

查看:80
本文介绍了在JQuery中分离是否会破坏DOM中的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JQuery中的detach方法是否实际上破坏了DOM中的节点?例如,如果我这样做:

Does the detach method in JQuery actually destroys the node in DOM? For example, if I do:

$("#myDiv").detach().appendTo($("#container"));

将销毁原始 #myDiv 节点,并创建一个新的节点来附加到 #container ?或者它将是同一个节点?

Will destroy the original #myDiv node, and create a new node to append in #container? Or it will be the same node?

使用destroy,我的意思是当我执行时,存储浏览器中的节点的内存位置被释放分离。或者JQuery确实保存了节点的引用(而不是副本),防止它被释放,并且可以在以后重新附加。

With destroy, I mean that the memory location that stores the node within the browser is freed when I execute detach. Or JQuery does save a reference (not a copy) of the node, preventing it to be freed, and be available to re-attach later?

推荐答案

$(#myDiv)对象保持属性 0 相关元素,即使在你把它分开了当你附加的时候,你真的附加了相同的元素,它不是一个新元素。

The $("#myDiv") object keeps as property 0 the relevant element, even after you detached it. When you append, you really append the same element, it's not a new one.

你可以通过注意到子元素再次被还原来检查。请注意,删除函数具有相同的行为,希望它清除链接到元素及其子元素的jQuery数据。您也可以通过发出 console.dir($(#myDiv)。detach())

You can check that by noticing that child elements are also restored again. Note that the remove function has the same behavior expect it cleans jQuery data linked to the element and its childs. You can also see your element(s) by issuing console.dir($("#myDiv").detach()).

这篇关于在JQuery中分离是否会破坏DOM中的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆