使用jQuery删除文本和标签

use*_*384 5 jquery removeall

听起来很简单,但它确实有效:我想删除两个span标签之间的所有内容(文本和标签):

<div class="comment-body" id="comment-body-parent-330">
<p><span id="sc_start_commenttext-330"></span>
Some Text<br>
Some Text<br>
Some Text</p>
<p>Some Text<span id="sc_end_commenttext-330"></span>
</p>
Some Text and Code
</div>
Run Code Online (Sandbox Code Playgroud)

您应该删除span id"sc_start_commenttext-330"和span id"sc_end_commenttext-330"之间的所有内容,如下所示:

    <p><span id="sc_start_commenttext-330"></span>
<span id="sc_end_commenttext-330"></span>
    </p>
Run Code Online (Sandbox Code Playgroud)

我只是尝试使用该代码,但它最有效:

    jQuery("#sc_start_commenttext-330").
nextUntil("#sc_end_commenttext-330").
remove();
Run Code Online (Sandbox Code Playgroud)

代码由CMS生成,因此无法更改HTML.

一些想法?非常感谢!

Bor*_*isD 1

这取决于你想要做什么......尝试这样的事情:

// for every "p" element
$("p").each(
  function () {
     // temprary save the children elements like spans & divs
     $temp = $(this).children("span,div");
     // clean the "p" container (text + <br>)
     $(this).empty();
     // recive the children
     $(this).append($temp);
   }
);
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,您可以控制要保留的元素(span,div)...然后删除所有其余元素。希望能帮助到你 :-)