如何删除< div>内的所有内容?

问题描述:

我有以下内容:

<div id="test">

...
...

</div>

我想删除div中的所有元素,所以我尝试了:

I would like to remove all of the elements within the div so I tried:

$('#test > div').remove();

但这似乎不起作用。我在这里做对了吗?

But this doesn't seem to work. Am I doing the right thing here?

尝试正确的语法

删除 :删除匹配的一组来自DOM的元素。

Remove : Remove the set of matched elements from the DOM.



 $('div#test').remove();

尝试

try with empty


:删除所有孩子来自DOM的匹配元素集的节点。

empty : Remove all child nodes of the set of matched elements from the DOM.



 $('#test').empty();

参见 html() ,有时也很有帮助

see html() also, sometime it is helpful


html: 当.html()用于设置元素的内容时,
在该元素中的任何内容都将被新内容完全替换。

html: When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content.

注意: 要删除元素而不删除数据和事件,请使用。detach()