Collapse all ULs other than the clicked one and its parent ULs

I have a navbar with nested ULs as shown further below. I'm using a simple class, and toggling it, to hide menus/submenus:

nav ul ul {
    display: none;
}
.showIt {
    display: block;
}

On clicking a link with href of "#" I'm trying to hide all the other ULs, except the ones containing the clicked item, using:

$("nav").on('click', "a[href='#']", function () {
    var $thisUl = $(this).next('ul');
    $('nav ul').not($thisUl).each(function () {
        if (!($.contains($(this), $thisUl)))
            $(this).removeClass('showIt');
    });
    $thisUl.toggleClass("showIt");
});

This isn't working for some reason, as it doesn't expand the clicked submenu. In fact, it collapses the menu containing this submenu.

Here's the nav structure for reference:

<nav>
<ul class="navbar">
    <li>
        <a href="index.html">Home</a>
    </li>
    <li>
        <a href="newsletter.html">News/Hints &amp; Tips</a>
    </li>
    <li>
        <a href="#">Courses</a>
        <ul>
            <li>
                <a href="#">IT Courses</a>
                <ul>
                    <li><a href="courses/access-main.html">Access</a></li>
                    <li><a href="courses/excel-main.html">Excel</a></li>
                    <li><a href="courses/onenote-main.html">OneNote</a></li>
                    <li><a href="courses/outlook-main.html">Outlook</a></li>
                </ul>
            </li>
            <li>
                <a href="#">Microsoft Certified</a>
                <ul>
                    <li><a href="ms-certified-courses/mos-main.html">Microsoft Office Specialist(MOS)</a></li>
                    <li><a href="ms-certified-courses/mosexpert-main.html">Microsoft Office Specialist(MOS) Expert</a></li>
                    <li><a href="ms-certified-courses/mosmaster-main.html">Microsoft Office Specialist(MOS) Master</a></li>
                </ul>
            </li>
            <li><a href="http://external_link.co.uk" target="_blank">PD Courses</a></li>
        </ul>
    </li>
    <li>
        <a href="#">Materials</a>
        <ul>
            <li><a href="courseware.html">Course Materials</a></li>
            <li><a href="materials/latest.html">Latest Downloads</a></li>
            <li><a href="materials/access.html">Access</a></li>
            <li><a href="materials/excel.html">Excel</a></li>
        </ul>
    </li>
    <li>
        <a href="testimonials.html">Testimonials</a>
    </li>
    <li><a href="ourteam.html">Our Team</a></li>
    <li><a href="contactus.html">Contact Us</a></li>
</ul>
</nav>

How can I collapse (remove the class of) all ULs other than the current one, and its parent ULs.


contains() requires dom elements

Just change

if (!($.contains($(this), $thisUl)))

to

if (!($(this)[0].contains($thisUl[0])))

(Demo)

And here is the vanilla javascript solution for future viewers.

(function () {
    "use strict";
    document.querySelector('nav').onclick = function (e) {
        if (e.target.getAttribute('href') == '#') {
            var thisUl = e.target.nextElementSibling;
            var lists = document.querySelectorAll('nav ul.showIt'),
                list;
            for (var i = 0; list = lists[i]; i++) {
                if (!list.isSameNode(thisUl) && !list.contains(thisUl)) list.className = list.className.replace(' showIt', '');
            }
            thisUl.className += ' showIt';
        }
    };
})();

(Demo)

链接地址: http://www.djcxy.com/p/83678.html

上一篇: 由于Bootstrap代码,无法隐藏很长的菜单项

下一篇: 折叠所点击的UL及其父母UL以外的所有UL