1

私はこのような汚い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>

divjQueryの最初の子タグの前にあるすべての (囲まれていない) テキスト要素を削除したいと考えています。

4

1 に答える 1

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

デモ。

于 2013-10-13T22:49:27.627 に答える