如何使用 JQuery 检索设置了特定类的所有标签的列表,然后从这些标签中删除内容

How can I use JQuery to retrieve the list of all the tags having a specific class setted and then remove the content from these tags?

本文关键字:标签 然后 列表 删除 设置 检索 JQuery 何使用      更新时间:2023-09-26

我还不太喜欢JavaScript和JQuery,我有以下问题。

进入HTML页面,我有一些包含一些内容的标签(标签的数字不是固定的,可以在运行时更改,如下所示:

<section id="mainSection" class="com__section com__section--text">
    .........................................................
    .........................................................
    MAIN SECTION CONTENT
    .........................................................
    .........................................................
</content>
<section class="com__section com__section--text functionSection">
    <h1 class="animate slideInRight" ">SECTION 1</h1>
    .........................................................
    .........................................................
</section>
<section class="com__section com__section--text functionSection">
    .........................................................
    .........................................................
    <h1 class="animate slideInRight" ">SECTION 2</h1>
    .........................................................
    .........................................................
</section>

如您所见,第一个标签具有id="mainSection",并且它始终存在于页面中。另一个则设置了class="functionSection"。

我的问题是我必须使用 JQuery 来检索所有设置了 class="functionSection" 的列表,并删除这些部分中的所有内容。

如何使用 JQuery 检索具有此函数节类的所有部分并删除其内容?(部分标签必须保留在我的 HTML 中,但必须为空)。

很简单!删除html内容

$('.functionSection').html("");

你想要防弹吗?迭代所有元素。

$('.functionSection').each(function() {
   $(this).html("");
});

$('.functionSection').html('');将使用类函数部分的任何标签中删除所有html内容 - 您还有什么希望它做的吗?

相关文章: