使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:45:44  来源:igfitidea点击:

Remove inner divs from a parent div using jquery

jqueryhtmlclear

提问by ACP

Consider my parent div is parentDivand 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 parentDivusing jquery....

如何parentDiv使用jquery清空子元素....

EDIT:

编辑

What is the diff between empty()and remove()? what should i use?

empty()和之间有什么区别remove()?我应该用什么?

采纳答案by AutomatedTester

$("#parentDiv").empty();from here

$("#parentDiv").empty();这里

回答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

removeremoves the element itself from the DOM (in this case #parentDiv) whereas emptyis 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()删除匹配选择器。