使用 jquery 从父 div 中删除内部 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2544298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove inner divs from a parent div using jquery
提问by ACP
Consider my parent div is parentDiv
and it contains five child divs
考虑我的父 div 是parentDiv
,它包含五个子 div
<div id="parentDiv">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
How to empty the child elements parentDiv
using jquery....
如何parentDiv
使用jquery清空子元素....
EDIT:
编辑:
What is the diff between empty()
and remove()
? what should i use?
empty()
和之间有什么区别remove()
?我应该用什么?
回答by Ivan
Have you tried $("#parentDiv div").remove()
or $("#parentDiv").empty()
?
您是否尝试过$("#parentDiv div").remove()
或$("#parentDiv").empty()
?
回答by Amber
.empty()
removes all of the children of the selected element(s); .remove()
removes the selected element(s) themselves as well as any children.
.empty()
删除所选元素的所有子元素;.remove()
删除所选元素本身以及任何子元素。
Thus, $("#parentdiv").empty();
makes the most sense here, because you want to remove the children but not the parent div.
因此,$("#parentdiv").empty();
在这里最有意义,因为您想删除子级而不是父级 div。
回答by Matt
remove
removes the element itself from the DOM (in this case #parentDiv) whereas empty
is equivilent to calling $('#parentDiv').children().remove();
and removes all of the elements children.
remove
从 DOM 中删除元素本身(在本例中为 #parentDiv),而empty
等同于调用$('#parentDiv').children().remove();
并删除所有元素的子元素。
In this case you should use empty
:
在这种情况下,您应该使用empty
:
$('#parentDiv').empty()
回答by rahul
empty()
removes all the child nodes of the matched selector, whereas remove()
removes the matched selector.
empty()
删除匹配选择器的所有子节点,而remove()
删除匹配选择器。