简体   繁体   中英

Display block parent class when element inside has class active

So, I have some problems with this. I have multiple parent divs with the same class and I want when an element inside every parent div has class "selected_tip" to display block the parent of the parent of that element. How to do this. Keep in mind that the id's are dinamic. My code is this:

 <div class="tip"> <div id="tip_1" class="tt tip1"> <p>Proaspete</p> <span class="select_tip select_copt "></span> </div> <div id="tip_2" class="tt tip2"> <p>Coapte, tocate</p> <span class="select_tip select_copt "></span> </div> <div id="tip_3" class="tt tip1"> <p>Călite</p> <span class="select_tip select_copt selected_tip"></span> </div> <div id="tip_4" class="tt tip2"> <p>Prăjite</p> <span class="select_tip select_copt "></span> </div> <div id="tip_5" class="tt tip1"> <p>Fierte</p> <span class="select_tip select_copt "></span> </div> </div> 

You can use .closest()

For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

$('.selected_tip').closest('div.tip').css('display', 'block')

OR, Use .parent() to traverse up to parent.

Get the parent of each element in the current set of matched elements, optionally filtered by a selector.

$('.selected_tip').parent().parent().css('display', 'block')

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