如何在第一个标记之前删除未包含的脏文本

时间:2013-10-13 22:44:14

标签: jquery html dom

我有一个看起来像这样的脏HTML

示例1:

<div>

  somedirtytexthere.

  <h1>header 1</h1>
  <p>ok this text is cool</p>
</div>

示例2:

<div>

  somedirtytexthere.

  <h1>header 1</h1>
  <p>ok this text is cool</p>
       but this text is ALSO cool
</div>

我想用jQuery删除div的第一个子标记前面的每个(未封闭的)文本元素。

1 个答案:

答案 0 :(得分:3)

$("div")
    .contents()
    .filter(function () {
        return this.nodeType === 3;
    }).first().remove();

Demo.

相关问题