简体   繁体   中英

Apply CSS rule to a group of elements between two other elements

<div class="aaa">
  <blockquote>...</blockquote>
  <p>...</p>
  <div class="bbb">...</div>
  <div class="ccc">...</div>
</div>

Is there a way to apply display: none to everything between aaa and bbb ? That is, to <blockquote> and <p> . (The contents is not limited to blockquote and p . For example, it could be <div> or <pre> .)

You will need two rules:

 .aaa > * { display:none; }.aaa >.bbb, .aaa >.bbb ~ * { display:block; }
 <div class="aaa"> <blockquote>block</blockquote> <p>ppp</p> <div class="bbb">bbb</div> <div class="ccc">ccc</div> </div>

If you want to use JavaScript (for example to apply this change programmatically), you can get all the children of the element with class aaa and then iterate over them applying the style, stopping when you reach the one with class bbb .

let parent = document.querySelector('.aaa');

for (let child of parent.children) {
    if (child.classList.contains('bbb'))
        break;

    child.style.display = 'none';
}

 #xxx{ display:none; }
 <div class="aaa"> <div id='xxx'> <blockquote>.zzz..</blockquote> <p>.yyy..</p> </div> <div class="bbb">...</div> <div class="ccc">...</div> </div>

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.

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