简体   繁体   English

删除 H1 之前的中断

[英]Removing Breaks Before H1

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?由于<h1>之前的<br />数量不同,我想以编程方式删除它们,我如何使用 jQuery 来做到这一点?

If you want to remove all of them before the h1 elements, do this:如果要在 h1 元素之前删除所有这些,请执行以下操作:

$('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.使用next-adjacent-selector [docs] ,这将找到前面至少有一个<br>元素的所有<h1>元素。

Then it uses the prevAll() [docs] method to select the previous <br> elements, and the remove() [docs] method to remove them.然后它使用prevAll() [docs]方法 select 之前的<br>元素,并使用remove() [docs]方法删除它们。

You can find them all with .prevAll() and .remove() them, like this:您可以使用.prevAll().remove()找到它们,如下所示:

$("#header").prevAll("br").remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM