Перемещение нескольких родителей в JQuery - более эффективный способ?

Итак, у меня есть навигация, которая является списком и имеет подсписки и подсписки.

По сути, навигация по умолчанию свернута, но если люди нажимают на страницу, котораяs в подсписке, я хочу показать родительский список. И если этоПодсписок подсписка, мне нужно показать оба родительских списка. Я настроил это, но я нене нравится помещать 5 .parent (). parent () 's, чтобы пройти вверх, чтобы расширить список. Есть ли более эффективный способ?

HTML:


        
            <a href="index.html">Welcome to Beat Basics and Beyond</a>
            <a href="table-of-contents.html">What's in this course?</a>
            <a href="defining-your-beat.html" class="active">Defining Your Beat</a>
                
                    <a href="boundaries-of-your-beat.html">Boundaries of Your Beat</a>
                    <a href="the-beat-description.html">The Beat Description</a>
                    <a href="build-your-own-beat-description.html"><span class="ital">Activity:</span> Build Your Own Beat Description</a>
                
            
            <a href="getting-started.html">Getting Started</a>
                
                    <a href="debrief-your-predecessor.html">Debrief Your Predecessor</a>
                    <a href="predecessor-top-five-tips.html"><span class="ital">Activity:</span> List The Top Five Tips From Your Predecessor</a>
                    <a href="covering-your-beat-with-the-internet.html">Covering Your Beat With The Internet</a>
                    <a href="get-in-the-car-and-go.html">Get in the Car and Go</a>
                    <a href="mapping-your-beat.html">Mapping Your Beat</a>
                    <a href="read-the-clips.html">Read the Clips</a>
                    <a href="activity-dissect-this-clip.html"><span class="ital">Activity:</span> Dissect This Clip</a>
                    <a href="writing-your-first-article.html">Writing Your First Article</a>
                    <a href="starting-cold-on-the-beat.html">Starting Cold on the Beat</a>
                           
            
            <a href="working-with-sources.html">Working With Sources</a>
                
                    <a href="finding-sources.html">Finding Sources</a>
                    <a href="diversify-your-sources.html">Diversify Your Sources</a>
                    <a href="prospecting-for-stories-and-sources.html">Prospecting for Stories and Sources</a>
                    <a href="building-relationships.html">Building Relationships</a>
                    <a href="going-off-the-record.html">Going Off the Record</a>
                
            
            <a href="developing-resources.html">Developing Resources to Help You on the Beat</a>
                
                    <a href="develop-a-calendar-of-events.html">Develop a Calendar of Events</a>
                    <a href="build-your-library.html">Build Your Library</a>
                    <a href="learn-the-open-record-laws.html">Learn the Open Record Laws</a>
                
            
            <a href="extra-resources.html">Extra Resources</a>
                
                    <a href="beat-breakdown-tool.html">Beat Breakdown Tool</a>
                    <a href="links-library.html">Links Library</a>
                        
                            <a href="general-resources-for-any-beat.html">General Resources for Any Beat</a>
                            <a href="courts-and-criminal-justice-links.html">Courts and Criminal Justice Links</a>
                            <a href="education-resources.html">Education Resources</a>
                            <a href="local-government-links.html">Local Government Links</a>
                            <a href="neighborhood-or-suburban-links.html">Neighborhood or Suburban Links</a>
                            <a href="police-and-public-safety-links.html">Police and Public Safety Links</a>
                            <a href="reporter-organizations.html">Reporter Organizations</a>
                        
                    
                    <a href="additional-reading.html">Additional Reading</a>
                
            
            <a href="final-thoughts.html">Final Thoughts</a>
        

JQuery:

function toggleSubmenu() {

    if ($(this).hasClass('submenu-hidden')) {

        $(this).parent().children('ul').slideToggle();
        $(this).removeClass().addClass('submenu-visible');

    } else if ($(this).hasClass('submenu-visible')) {

        $(this).parent().children('ul').slideToggle();
        $(this).removeClass().addClass('submenu-hidden');
    }
}

$('#lesson-sidebar ul ul').hide();
$('#lesson-sidebar ul ul ul').hide();
$('#lesson-sidebar ul:first-child').attr('id', 'rootlist');
$('#lesson-sidebar ul li:has("ul")').prepend('<span class="submenu-hidden"></span>').css('list-style','none');

$('#lesson-sidebar ul li a').each(
    function() {
        if ($(this).hasClass('active')) {
            // if it is a UL
            var length = $(this).parent().find("ul").length;
            alert(length);
            if (length == 0) {
                if ($(this).parent().parent().parent().children('span').hasClass('submenu-hidden')) {
                        $(this).parent().parent().parent().children('ul').show();
                        $(this).parent().parent().parent().children('span').removeClass('submenu-hidden').addClass('submenu-visible');
                }
                if ($(this).parent().parent().parent().parent().parent().children('span').hasClass('submenu-hidden')) {
                        $(this).parent().parent().parent().parent().parent().children('ul').show();
                        $(this).parent().parent().parent().parent().parent().children('span').removeClass('submenu-hidden').addClass('submenu-visible');
                } 
            }
            if (length == 1) {
                $(this).parent().find('ul').slideToggle();
                $(this).parent().children('span').removeClass('submenu-hidden').addClass('submenu-visible');
            }               
        }
    }
);

$('ul#rootlist > li span, ul#rootlist li ul li > span').bind('click', toggleSubmenu);

Любая помощь приветствуется.

Ответы на вопрос(5)

Ваш ответ на вопрос