Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

html - Twitter Bootstrap multiple accordions but only one open panel at a time

I have 3 Twitter Bootstrap accordions on my page, each one set up like this:

<div class="panel-group" id="accordion1">
    <div class="panel panel-default">
        <div class="panel-heading accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion1" data-target="#collapseOne1">
             <h4 class="panel-title">Collapsible Group Item #1</h4>
        </div>
        <div id="collapseOne1" class="panel-collapse collapse">
            <div class="panel-body">...</div>
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion1" data-target="#collapseTwo1">
             <h4 class="panel-title">Collapsible Group Item #2</h4>
        </div>
        <div id="collapseTwo1" class="panel-collapse collapse">
            <div class="panel-body">...</div>
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion1" data-target="#collapseThree1">
             <h4 class="panel-title">Collapsible Group Item #3</h4>
        </div>
        <div id="collapseThree1" class="panel-collapse collapse">
            <div class="panel-body">...</div>
        </div>
    </div>
</div>

The id's of the accordions are like the one displayed above, so #accordion1, #accordion2 and #accordion3. The same counts for the panels per accordion.

How can I alter this set up so that only one panel can be open at a time? At the moment, one panel per accordion can be open at a time, which is not my desired behaviour.

DEMO: JSFiddle

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The accepted answer is good, but doesn't exactly answer what they did in order to make that work.

In this fiddle you can see the bare-minimum code that's needed, which is:

 <div class="panel-group" id="panels">
    <div class="panel panel-default">
        <div class="collapsed" data-toggle="collapse" data-parent="#panels" data-target="#firstPanel">
             <h4>First header</h4>    
        </div>
        <div id="firstPanel" class="panel-collapse collapse">
            <div>Content.</div>
        </div>
    </div>
    <div class="panel panel-default">
        <div class="collapsed" data-toggle="collapse" data-parent="#panels" data-target="#secondPanel">
             <h4>Second header</h4>

        </div>
        <div id="secondPanel" class="panel-collapse collapse">
            <div>Other content.</div>
        </div>
    </div>
</div>

Notice that each panel has a data-parent which targets the parent div with the ID #panels.

That is the magic sauce you need.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...