javascript - jquery 可折叠菜单帮助 - 差不多了,但也需要 2 层深度可折叠!

标签 javascript jquery

我从 http://www.i-marco.nl/weblog/ 获取了 Accordion 菜单并根据我的需要定制了它。我遇到的一个问题是我有几个子菜单项也是可折叠的。我的代码在折叠菜单方面工作得很好,但是当单击父元素时我无法让它缩回此菜单。谁能看出我哪里出了问题吗?

提前致谢!

HTML 片段:

    <ul class='menu collapsible' id='menu'> 

                                   <li id="home" class="selected expand top"> 
                                        <a href="http://localhost//public_html/index.php/home">Home</a> 
                                   </li> 
                                   <li id="about_the_school" class="top"> 
                                        <a href="http://localhost//public_html/index.php/about_the_school">About the School<span>Click to expand</span></a> 
                                        <ul class='acitem'> 
                                             <li> 
                                                  <a href="http://localhost//public_html/index.php/about_the_school/welcome">Welcome</a> 
                                             </li> 

                                        </ul> 
                                   </li> 
                                   <li id="academic" class="top"> 
                                        <a href="http://localhost//public_html/index.php/academic">Academic<span>Click to expand</span></a> 
                                        <ul class='acitem'> 
                                             <li> 
                                                  <a href="http://localhost//public_html/index.php/academic/curriculum_&amp;_exam_system">Curriculum &amp; Exam System</a> 
                                             </li> 
                                             <li> 
                                                  <a href="http://localhost//public_html/index.php/academic/departments">Departments</a> 
                                                  <ul class='acitem menu collapsible'> 
                                                       <li> 
                                                            <a href="http://localhost//public_html/index.php/academic/departments/art">Art</a> 
                                                       </li> 
                                                       <li> 
                                                            <a href="http://localhost//public_html/index.php/academic/departments/business_education">Business Education</a> 
                                                       </li> 

                                                  </ul> 
                                             </li> 
                                             <li> 
                                                  <a href="http://localhost//public_html/index.php/academic/pupil_support">Pupil Support</a> 
                                             </li> 
                                             <li> 
                                                  <a href="http://localhost//public_html/index.php/academic/careers">Careers</a> 
                                             </li> 
                                        </ul> 
                                  </li> 
                              </ul>
</ul>

JS:

   jQuery.fn.initMenu = function(){
    return this.each(function(){
        var theMenu = $(this).get(0);
        $('.acitem', this).hide();
        $('li.expand > .acitem', this).show();
        $('li.expand > .acitem', this).prev().addClass('active');
        $('li a', this).click(function(e){
            e.stopImmediatePropagation();
            var theElement = $(this).next();
            var parent = this.parentNode.parentNode;

            if (theElement.hasClass('acitem') && theElement.is(':visible')) {
                 if ($(parent).hasClass('collapsible')) {
                        $('.acitem:visible', parent).first().slideUp('normal', function(){
                            $(this).prev().removeClass('active');
                        });
                        return false;
                    }
                    return false;
            }
            if (theElement.hasClass('acitem') && !theElement.is(':visible')) {

                //custom - adds class at beginning of expansion
                $(this).addClass('active');

                $('.acitem:visible', parent).first().slideUp('normal', function(){
                    $(this).prev().removeClass('active');
                });
                theElement.slideDown('normal', function(){
                      $(this).prev().addClass('active');
                 });
                return false;
             }
        });
    });
};$(document).ready(function(){
    $('.menu').initMenu();
});

最佳答案

在此部分:

<a href="http://localhost//public_html/index.php/academic">Academic
  <span>Click to expand</span>
</a> 
<ul class='acitem'> 

您需要一个collapsible类(class)<ul>要让它也折叠起来,与其他人一样,如下所示:

<ul class='acitem collapsible'>

You can see an updated/working demo with the fix here .

关于javascript - jquery 可折叠菜单帮助 - 差不多了,但也需要 2 层深度可折叠!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3052295/

相关文章:

javascript - 如何在 Angular JS 中添加图像

javascript - 如果我使用 "id",我的 fancybox 将打开,但如果我使用 "class",则不会打开

javascript - for...in 循环和 jQuery each() 函数有什么区别?

javascript - 什么是 “jQuery.event.special” ?

javascript - 在窗口调整大小时重新计算表格高度

javascript - 在 TypeScript 中使用 "is"关键字有什么好处?

javascript - x = function(a, b, c){} 和 function x(a, b, c){} 有什么区别?

javascript - DeviantArt 的脚本负责发送个人资料评论

javascript - jQuery 1.11.3 : Insert a div of "Tasks" based on the order of their categories

jquery - 使用 CSS 动画围绕地球旋转点