Collapse all other accordion items when one is exp

2019-07-27 00:48发布

问题:

I have an accordion menu that works nicely. But now, I want only one parent item expanded at a time. In other words, when I click to expand one parent item, any other expanded parent item needs to collapse. I thought adding a line to slide the sibling elements up would work, but either that's not it, or I'm doing it wrong. Any ideas?

Here is my jsfiddle

Here is my code:

CSS

a
    {
        cursor: pointer;
    }

.menu ul 
    {
        list-style: none outside none;
    }

.menu li a 
    {
        line-height: 25px;
    }

.menu > ul > li > a 
    {
        color: #3B4C56;
        display: block;
        font-weight: normal;
        position: relative;
        text-decoration: none;
    }

.menu li.parent > a 
    {
        padding: 0 0 0 28px;
    }

.menu li.parent > a:before 
    {
        background-image: url("../images/plus_minus_icons.png");
        background-position: 25px center;
         content: ""; 
        display: block;
        height: 21px;
        left: 0;
        position: absolute;
        top: 2px;
        vertical-align: middle;
        width: 23px;
    }

.menu ul li.active > a:before 
    {
        background-position: 0 center;
    }

.menu ul li ul 
    {
        border-left: 1px solid #D9DADB;
        display: none;
        margin: 0 0 0 12px;
        overflow: hidden;
        padding: 0 0 0 25px;
    }

.menu ul li ul li 
    {
        position: relative;
    }

.menu ul li ul li:before 
    {
        border-bottom: 1px dashed #E2E2E3;
        content: "";
        left: -20px;
        position: absolute;
        top: 12px;
        width: 15px;
    }

jQuery

$( document ).ready( function( ) {
        $( '.menu li' ).each( function() {
                if( $( this ).children( 'ul' ).length > 0 ) {
                        $( this ).addClass( 'parent' );     
                }
        });

        $( '.menu li.parent > a' ).click( function(event) {

            if($(this).next('ul').is(':hidden'))
            {
                event.preventDefault();            

                $( this ).parent().toggleClass( 'active' );
                $( this ).parent().children( 'ul' ).slideDown( 'fast' );
                $( this ).parent().children.siblings( 'ul' ).slideUp( 'fast' );
            }
        });

});

HTML

<div class="menu">
    <ul>
        <li><a href="http://www.google.com">Menu item 1</a>
            <ul>
                <li><a>Menu item 1.1</a></li>
                <li><a>Menu item 1.2</a>
                    <ul>
                        <li><a>Menu item 1.2.1</a></li>
                        <li><a>Menu item 1.2.2</a></li>
                        <li><a>Menu item 1.2.3</a></li>
                    </ul>
                </li>
                <li><a>Menu item 1.3</a></li>
            </ul>
        </li>
        <li><a href="http://www.apple.com">Menu item 2</a>
            <ul>
                <li><a>Menu item 2.1</a></li>
                <li><a>Menu item 2.2</a>
                    <ul>
                        <li><a>Menu item 2.2.1</a></li>
                        <li><a>Menu item 2.2.2</a></li>
                        <li><a>Menu item 2.2.3</a></li>
                    </ul>
                </li>
                <li><a>Menu item 2.3</a></li>
            </ul>
        </li>
        <li><a href="http://www.twitter.com">Menu item 3</a>
            <ul>
                <li><a>Menu item 1.1</a></li>
                <li><a>Menu item 1.2</a>
                    <ul>
                        <li><a>Menu item 1.2.1</a></li>
                        <li><a>Menu item 1.2.2</a></li>
                        <li><a>Menu item 1.2.3</a></li>
                    </ul>
                </li>
                <li><a>Menu item 1.3</a></li>
            </ul>
        </li>
    </ul>
</div>

回答1:

You have to make this addition:

$(this).parent().siblings().find('ul').slideUp('fast');

Fiddle: http://jsfiddle.net/7gxaprtt/7/