1

I'm trying to remove the last TWO instances of the break tags for the following code:

<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

Try:

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

This uses the :last selector to get the last matching element and remove it.

Edit
I also updated the class selector. Here's a jsFiddle.

Edit 2 It would be more efficient if you gave the table (or even the table cell) an ID and used that in the selector instead but you probably won't notice any difference if the HTML is this simple.

于 2011-05-05T14:34:21.467 回答
1

This will work if the <br>s are always the siblings of the table .addcart.

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

Try:

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

Try this:

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