gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:44:38 26 4
gpt4 key购买 nike

如何使用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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com