Removing Breaks Before H1
I have a page that is li开发者_如何转开发ke this:
<div id="contBody">
<br />
<br />
<!-- Maybe more, it's a variable number of <br /> that can appear -->
<h1 id="header">Test</h1>
</div>
Since the number of <br />
before the <h1>
varies I and I want to remove them programmatically, how I can do it using jQuery?
If you want to remove all of them before the h1 elements, do this:
$('br + h1').prevAll('br').remove();
Using the next-adjacent-selector
[docs], this will find all <h1>
elements that are preceded by at least once <br>
element.
Then it uses the prevAll()
[docs] method to select the previous <br>
elements, and the remove()
[docs] method to remove them.
You can find them all with .prevAll()
and .remove()
them, like this:
$("#header").prevAll("br").remove();
精彩评论