使用jQuery更改家长的班级 [英] Change parent's class with jQuery

查看:70
本文介绍了使用jQuery更改家长的班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些链接有下拉列表组.这是他们的一部分:

There are dropdown groups for certain links. This is the part of them:

如果li的活动项目级别在1级以下,那么我尝试用open-item替换父li的关闭项目.

If the li has active-item class under level-1, then i try to replace parent li's close-item with open-item.

这是为此的jquery代码.

This is jquery code for this.

$(document).ready(function() {

  $('.active-item').parent('li').addClass('open-item').removeClass('close-item');
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="has-child-item close-item">
  <a><i class="fa fa-cubes" aria-hidden="true"></i><span>İstasyon Ayarları</span></a>
  <ul class="nav child-nav level-1">
    <li class="active-item"><a href="{{url('/istasyonlar')}}">İstasyon Listesi</a></li>
    <li><a href="{{url('/istasyonlar/ekle')}}">Yeni İstasyon Tanımlama</a></li>
  </ul>
</li>

但是它不起作用.

推荐答案

.active-item不是li的父级,而是li,您需要:

.active-item is not a parent of li it IS the li, you want:

.closest("li.close-item")

这篇关于使用jQuery更改家长的班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆