对 comment
节点进行深度
克隆,并在检索
reply
之前删除
innerHTML
节点:
var comment = document.querySelector(".comment").cloneNode(true);
comment.querySelector(".reply").remove();
console.log(comment.innerHTML);
<span class="comment">
EXAMPLES:
<p><i>Example 1</i> - <a href="https://example.com/1">https://example.com/1</a> - Jan 2020 (2 comments)</p>
<p><i>Example 2</i> - <a href="https://example.com/2">https://example.com/2</a> - Jun 2022 (13 comments)</p>
<div class="reply"><p><font size="1"><u><a href="reply?id=12323" rel="nofollow">reply</a></u></font></p></div>
<p><i>Example 3</i> - <a href="https://example.com/3">https://example.com/3</a> - Apr 2023 (33 comments)</p>
</span>
注意我在您的示例中添加了缺少的结束
</font>
标签。如果没有这个,无与伦比的开口 <font>
将保留在 innerHTML
中。