简体   繁体   English

jquery UL LI 折叠其下所有展开的子项

[英]jquery UL LI collapse all of the expanded child under it

i have a list order looking like these:我有一个看起来像这样的列表顺序:

<ul>
    <li>level 1 a
        <ul>
            <li>level 2 a
                <ul>
                    <li>level 3 a</li>
                    <li>level 3 b</li>
                </ul>
            </li>
            <li>level 2 b</li>
        </ul>
    </li>
    <li>level 1 b
        <ul>
            <li>level 2 c
                <ul>
                    <li>level 3 c</li>
                    <li>level 3 c</li>
                </ul>
            </li>
            <li>level 2 d</li>
            <li>level 2 e</li>
        </ul>
    </li>
</ul>

and jquery looking like these:和 jquery 看起来像这样:

$(document).ready(function(){
  $('li').click(function(ev) {
    ev.stopPropagation();
      $(this).children('ul').show();
      $(this).prev().children('ul').hide();
      $(this).siblings().children('ul li ul').hide();
      $(this).next().children('ul').hide();
  }); 
  $('li > ul').hide();
});

current situation:现在的情况:

1) i click on level 1 a then level 2 a , now level 1 a and level 2 a are expanded 1) 我点击level 1 a然后level 2 a ,现在 level 1 a 和 level 2 a 展开

2) i click on level 1 b , then i click back the level 1 a , i still see the level 1 a and its child is expanded. 2) 我点击level 1 b ,然后我点击level 1 a ,我仍然看到 level 1 a 并且它的子级被展开了。

the outcome i want is:我想要的结果是:

when i click back level 1 a, it will collapse everything, instead of showing everything that i clicked before, same goes to level 1 b, if i click that 1st.当我单击返回级别 1 a 时,它将折叠所有内容,而不是显示我之前单击的所有内容,如果我单击第 1 个级别,则同样转到级别 1 b。

jsfiddle提琴手

any help would be great.任何帮助都会很棒。 thanks谢谢

You can call hide on children ul whenever a sibling li is clicked using this每当使用此单击sibling li时,您都可以调用隐藏children ul

 $('li').click(function(ev) {
    $(this).siblings('li').click(function() {
      $(this).siblings('li').children('ul').hide()
    })

 $(document).ready(function() { $('li').click(function(ev) { $(this).siblings('li').click(function() { $(this).siblings('li').children('ul').hide() }) $(this).children('ul').children('li').children('ul').hide() ev.stopPropagation(); $(this).parent('ul').show(); $(this).children('li').hide(); $(this).siblings('li').children('ul li ul').hide(); $(this).children('ul').show(); $(this).prev('li').children('ul li ul').hide(); $(this).next().children('ul').hide(); }); $('li > ul').hide(); });
 current situation: 1) i click on level 1 a then level 2 a, now level 1 a and level 2 a are expanded 2) i click on level 1 b, then i click back the level 1 a, i still see the level 1 a and its child is expanded. the outcome i want is: when i click back level 1 a, it will collapse everything, instead of showing everything that i clicked before, same goes to level 1 b, if i click that 1st.
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul> <li>level 1 a <ul> <li>level 2 a <ul> <li>level 3 a</li> <li>level 3 b</li> </ul> </li> <li>level 2 b</li> </ul> </li> <li>level 1 b <ul> <li>level 2 c <ul> <li>level 3 c</li> <li>level 3 c</li> </ul> </li> <li>level 2 d</li> <li>level 2 e</li> </ul> </li> </ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM