Javascript 其中,将此添加到活动的仅一个li

Javascript 其中,将此添加到活动的仅一个li,javascript,Javascript,当我删除$this.closest时,它工作,但所有ul都被激活 html: closest()不起作用,因为您的子菜单不是arrow toggle元素的父菜单 尝试使用: $(this).closest('li').find('.ul unactive').toggleClass('ul-active') 它向上搜索DOM树中最近的父级,然后从那里向下搜索DOM树以查找您的.ul unactive元素。请将相关HTML代码添加到您的问题中。 <script type="text/java

当我删除
$this.closest
时,它工作,但所有
ul
都被激活

html:

closest()
不起作用,因为您的子菜单不是arrow toggle元素的父菜单

尝试使用:

$(this).closest('li').find('.ul unactive').toggleClass('ul-active')


它向上搜索DOM树中最近的
  • 父级,然后从那里向下搜索DOM树以查找您的
    .ul unactive
    元素。

    请将相关HTML代码添加到您的问题中。
    <script type="text/javascript">
        $(document).ready(function () {
    
            $('.arrow-toggle.glyphicon.glyphicon-menu-down').click(function () {
                $('.glyphicon-menu-down').toggleClass('up');
                $('.ul-unactive').toggleClass('ul-active');
            });
        });
    </script>
    
    $('.arrow-toggle.glyphicon.glyphicon-menu-down').click(function () {
        var $this = $(this);
    
        $this.closest('.glyphicon-menu-down').toggleClass('up')
        $this.closest('.ul-unactive').toggleClass('ul-active')
    })
    
    <li>
                          {% if gn.Nodes -%}
                              <span class="menu-toggle"><a href="{{gn.Url}}" class="">{{ gn.Name }}</a> <span class="arrow-toggle glyphicon glyphicon-menu-down"></span></span> 
                          {% else %}
                              <a href="{{gn.Url}}">{{ gn.Name }}</a>
                          {% endif %}
    
                          {% if gn.Nodes -%}
                              <ul class="ul-unactive">
                              {% for gnn in gn.Nodes -%}
                                  <li>{{ gnn | A }} </li>
                              {% endfor -%}    
                              </ul>
                          {% endif -%}
                      </li>
    
        .ul-unactive{
      max-height:0;
      transition: .5s;
      width:100%;
    
    }
    .ul-active{
      max-height:500px;
      transition: max-height 1s ease-in;
      width:100%;
    
    }