I am trying to get a boolean that will let me know if a parent element has a child element. Some of the constraints are:
If the box does not have a footer element, add a class
<div class="data-trend"> <header></header> <div class="main"></div> <footer></footer> </div>
<div class="data-trend"> <header></header> <div class="main"></div> <footer></footer> </div>
<div class="data-trend"> <header></header> <div class="main"> <span class="glyphicons"><span> <!-- On the glyphicons I would add class if there is no footer --> </div> <!-- notice no footer --> </div>
Moved the above comment into an actual answer:
var elems = document.querySelectorAll('.data-trend'), len = elems.length, i = -1 while(++i < len) { if(!elems[i].querySelector('footer')) elems[i].classList.add('no-footer') }
.no-footer { border: 3px solid blue; }
<div class="data-trend"> <header>Testing</header> <div class="main"></div> <footer>Footer</footer> </div> <div class="data-trend"> <header>Testing</header> <div class="main"></div> <footer>Footer</footer> </div> <div class="data-trend"> <header>Testing</header> <div class="main"> <span class="glyphicons"><span> </div> </div>
If you're doing a lot of this in vanilla, it makes sense to define a forEach
helper function for yourself
function forEach(elems, fn){
var len = elems.length,
i = -1
while(++i < len) {
fn(elems[i])
}
}
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.