简体   繁体   English

在li内切换和动画化ul的高度

[英]Toggle and animate height of a ul within a li

I have a list inside a li which needs to slide into view when the parent li is clicked. 我在li内有一个列表,单击父li时,该列表需要滑入视图。

My code works nicely but if i click any li all of the sub lists show where as i want it only to apply to the one that was clicked... 我的代码运行良好,但是如果我单击任何li,所有子列表都将显示在我希望的位置,仅适用于单击的那个...

$("#offering li").click(function() {
        $("#offering li ul").animate({height: "toggle"}, 1000);
    });


<ul id="offering">
     <li class="t current"><a href="#solutions"><span>sage solutions</span></a>
                    <ul>
                        <li><a href="#">50</a></li>
                        <li><a href="#">200</a></li>
                        <li><a href="#">CRM</a></li>
                    </ul>
                </li>
     <li class="m"><a href="#management"><span>solutions</span></a>
                  <ul>
                      <li><a href="#">50</a></li>
                      <li><a href="#">200</a></li>
                      <li><a href="#">CRM</a></li>
                  </ul>
              </li>
     <li class="b"><a href="#thirdparty"><span>third party additions</span></a></li>
            </ul>

$(this).find("ul").animate({height: "toggle"}, 1000);

Here you are applying the animation to all elements that match the selector #offering ul li, when infact you just need to apply it to the child ul of the li clicked. 在这里,您要将动画应用于与选择器#offering ul li匹配的所有元素,实际上,您只需要将其应用于单击的li的子ul。

Instead of the following 代替以下

$("#offering li").click(function() {
        $("#offering li ul").animate({height: "toggle"}, 1000);
    });

Try this 尝试这个

$("#offering li").click(function() {
        this.childNodes[0].animate({height:"toggle"},1000);
    });

I'm not as familiar with jQuery as I am with Mootools, so there may be a more appropriate way to get the child ul element than using the childNodes array - but you get the idea. 我对jQuery的了解不如对Mootools的熟悉,因此获取子ul元素可能比使用childNodes数组更合适的方法-但您知道了。

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

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