jquery - 如何使用jquery删除子元素和空格?

标签 jquery dom-manipulation

如何使用jquery删除子元素和空格?

我有以下内容:

<div class="sectionA"> <p>sfadfafdafdafdaf</p>  </div>

如果我这样做:

$(".sectionA *").remove();

我明白了:

<div class="sectionA">   </div>

删除 <p> 后如何删除空格子元素?

最佳答案

您需要的是.empty()方法,它的作用正是您所描述的:

This method removes not only child (and other descendant) elements, but also any text within the set of matched elements. This is because, according to the DOM specification, any string of text within an element is considered a child node of that element.

示例:

 $(".sectionA").empty();

jsFiddle Demo

关于jquery - 如何使用jquery删除子元素和空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14510631/

相关文章:

javascript - Bootstrap Accordion 与内部 Accordion 与不同的 shown.bs.collapse 事件

jQuery Validate - 我可以在更改一组字段后重新验证一组字段吗?

jquery - 如何从 Ajax 完成 promise 中报告额外的拒绝

javascript - 限制 HTML5 中的可拖放行

javascript - slideToggle 一个 div 并在另一个 div 中移动一个按钮

javascript - 如何将此字符串转换为某个数组?

javascript - 基于条件的动态行选择

angular - 为什么使用 ViewContainerRef 而不是 *ngif?

javascript - 当脚本插入 DOM 时,jQuery .on() 未绑定(bind)

javascript - 使用 append() 不会替换以前的内容