Tell me how to add a class to the li element (parent with parent class), only if the user is on the page, in this case, souvenirs or on the pages of child links (nested ul ), and when switching to other pages, the class was deleted, I break my head for 2 hours I can not figure out how to implement. Here is the html:

  <li class="item-176 deeper parent"> <a href="/korporativnaya-reklama/suveniry">Сувениры</a> <ul class="nav-child unstyled small"> <li class="item-177"><a href="/korporativnaya-reklama/suveniry/ruchki">Ручки</a></li> <li class="item-178"><a href="/korporativnaya-reklama/suveniry/magnity">Магниты</a></li> <li class="item-181"><a href="/korporativnaya-reklama/suveniry/fleshki">Флешки</a></li> <li class="item-182"><a href="/korporativnaya-reklama/suveniry/posuda">Посуда</a></li> <li class="item-183"><a href="/korporativnaya-reklama/suveniry/chasy">Часы</a></li> <li class="item-184"><a href="/korporativnaya-reklama/suveniry/zonty">Зонты</a></li> <li class="item-185"><a href="/korporativnaya-reklama/suveniry/futbolki-kepki">Футболки/кепки</a></li> <li class="item-186"><a href="/korporativnaya-reklama/suveniry/pledy-podushki">Пледы/подушки</a></li> <li class="item-187"><a href="/korporativnaya-reklama/suveniry/chto-to-neobychnoe">Что-то необычное</a></li> </ul> </li> 

Here is jquery, which adds a class to the current element, how can I write the condition I need here?

 jQuery(document).ready(function(){ jQuery('.nav11 li').each(function () { if (this.getElementsByTagName("a")[0].href == location.href) this.className = "active"; }); }); 

    1 answer 1

     jQuery(document).ready(function(){ if(/suveniry/.test(location.href)) jQuery('.nav11 li').each(function () { if (this.getElementsByTagName("a")[0].href == location.href) this.className = "active"; }); });