如何删除< div>内的所有内容? [英] How can I remove everything inside of a <div>

查看:124
本文介绍了如何删除< 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()

这篇关于如何删除&lt; div&gt;内的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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