jQuery prev, prevUntil or closest break tag then remove
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();
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.
This will work if the <br>
s are always the siblings of the table .addcart
.
$('.addcart').siblings('br').slice(-2).remove();
- jsFiddle Demo
- Docs for
.slice()
Try:
$(".theprices br").eq($(".theprices br").length - 1).remove();
$(".theprices br").eq($(".theprices br").length - 1).remove();
Try this:
$('.addcart').parent().children('br:last').remove();
$('.addcart').parent().children('br:last').remove();
精彩评论