1

次のコードのブレーク タグの最後の 2 つのインスタンスを削除しようとしています。

<table class="theprices">
  <tbody>
    <tr>
      <td>
        <a href="">Link</a>
        <font>stupid font tag</font>
        <br>
        <b>Something Bold</b>
        <br>
        <br>
        <a href="">Hidden Link</a>
        <table class="addcart"></table>
        <div>some div</div>
        <div>some other div</div>
      <td>
    </tr>
  </tbody>
</table>

$('.addcart').closest('br:nth-child(1)').remove();
$('.addcart').closest('br:nth-child(2)').remove();        
4

4 に答える 4

4

試す:

$('table.theprices br:last').remove();
$('table.theprices br:last').remove();

これは、:lastセレクターを使用して、最後に一致する要素を取得し、それを削除します。

編集
クラスセレクターも更新しました。これがjsFiddleです。

編集2 テーブル(またはテーブルセル)にIDを指定し、代わりにセレクターでそれを使用すると、より効率的ですが、HTMLがこれほど単純な場合は、おそらく違いに気付かないでしょう。

于 2011-05-05T14:34:21.467 に答える
1

これは、<br>s が常にテーブルの兄弟である場合に機能します.addcart

$('.addcart').siblings('br').slice(-2).remove();
于 2011-05-05T14:39:07.060 に答える
0

試す:

$(".theprices br").eq($(".theprices br").length - 1).remove();
$(".theprices br").eq($(".theprices br").length - 1).remove();
于 2011-05-05T14:35:18.213 に答える
0

これを試して:

$('.addcart').parent().children('br:last').remove();
$('.addcart').parent().children('br:last').remove();
于 2011-05-05T14:40:51.250 に答える