I have a page that is like 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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.