How to remove the first two BR tags with jquery?
I'd like to remove only the first two BR tags with jquery.
<div id="system">
<BR CLEAR="ALL"><BR>// I want to remove both BR.
...
开发者_如何转开发
<BR>...
...
<BR>
I assume something like this. But I am not sure.
$('#system br').remove();
Could anyone tell me how to do this please?
Use :lt()
:
$('#system br:lt(2)').remove();
:lt()
takes a zero-based integer as its parameter, so 2 refers to the 3rd element. So :lt(2)
is select elements "less than" the 3rd.
Example: http://jsfiddle.net/3PJ5D/
Try
$('#system br:first').remove();
$('#system br:first').remove();
The first line removes the first br
, then the second br
becomes the first, and then you remove the first again.
$("#system br:lt(2)").remove();
Also try nth-child.
$("#system > br:nth-child(1), #system > br:nth-child(2)").remove();
removes first and second instance of br within #system
Additionally if you only want to remove the last br only you can try;
$('#elementID br:last').remove();
Lets say that the br follows a , you can try
$('#elementID strong+br:last').remove();
Try it. Replace (max 4 BR) to (1 BR)
$('#system br').each(function() {
if ($(this).next().is('br')) {
$(this).next().remove();
}
});
KS FIDDLE
精彩评论