简体   繁体   中英

Give element class when specific element has active class

Is it possible with Jquery to add another class to the .body element when the third li element has an .active class? It cant just add an class based on the .active class, it has to be the .active on the third li element specifically.

<div id="extendedinfobox">
   <ul class="prodtabmenu">
    <li></li>
    <li></li>
    <li class="active"></li>
    <li></li>
    <li></li>
  </ul>
 </div>
<div class="body">

https://jsfiddle.net/sn2nc9bd/1/

if($('ul.prodtabmenu').find('li:nth-child(3)').hasClass('active'))
{
   $('.body').addClass('newClass');   
}

 if ($('ul.prodtabmenu').find('li:nth-child(3)').hasClass('active')) { //$('body').addClass('disabled'); $('.body').addClass('disabled'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div id="extendedinfobox"> <ul class="prodtabmenu"> <li>2</li> <li>21</li> <li class="active">3</li> <li>4</li> <li>1</li> </ul> </div> <div class="body"> 

Try like this

Use .nth-child()

Description: Selects all elements that are the nth-child of their parent.

use .nth-child(3) to select the 3rd li

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