English 中文(简体)
jQuery 删除函数是否真的删除 Dom 元素?
原标题:Does jQuery remove function really remove Dom elements?

I am really wondering if jQuery remove function really remove elements from DOM.
First, I looked here but the answers are not convincing.
I encountered this problem when I noticed I am still able to manipulate elements on which I have called remove function.

我的代码:

<div id="container">
    <div id="div">
        This is a div
    </div>
</div>

var div = $( #div );
$( #div ).remove();
$( #container ).append(div);

Note: My question is not how to solve this? but I want to understand what s going on here!

Actually, this code doesn t remove the #div from the dom, but if I have any data set to the #div, it will be lost. I am pretty confused now about the behaviour of remove function. Can anyone explain this please? DEMO

I am convinced that div variable is not just a clone of the dom element, is a reference to it, because when I manipulate the div variable, (like div.html( something )) the div within the DOM get updated.
Or am I wrong?

最佳回答

remove () 确实将元素从 DOM 中删除 。

然而,在您的示例中,由于您指定给 div 变量,该元素已被缓存。 因此您仍然可以使用它, 并再次附加它, 并且它仍然会包含原始的所有事件 。

如果你说对了,为什么我把数据 解密给Div呢?

如果您检查", http://james.padolsey.com/jquery/#v=git&fn=jQuery.fn.remove" rel=“ norefererr” > 源代码可以删除( 您可以看到它被称为内部 cleanData 函数, 来清除事件并清除元素的 data 缓存。 这就是为什么您丢失了信息 。

如果您想要从 DOM 中删除元素, 但保留元素, 请使用 < code> detach () 代替 < code > 。

此处显示差异:http://jsfiddle.net/2VbmX/

问题回答

必须删除指定的变量 :

delete div;




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签