Bootstrap Collapse Accordion一个面板打开,所有其他面板关闭

时间:2016-05-05 15:18:48

标签: javascript twitter-bootstrap-3

我创造了两个自举手风琴,一个在另一个之下。当我将它们全部作为一个手风琴时,我不喜欢面板笨拙地移动位置的方式,因此我创建了两个独立的手风琴。在这样做时,每支手风琴都按照我想要的方式工作:每个手风琴一次只能打开一个面板体,但我想只允许一个面板体打开..

更好地解释我的意思...... 目前,您可以在顶行和底行中打开一个面板主体。我希望你能够一次打开一个。

这可能吗?

以下是我所做的一个例子 https://codepen.io/aahmed2/pen/yOQvVz

这是我的代码。

<div class="row">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">

    <div class="panel panel-default col-md-4">
        <div class="panel-heading  bites-stings" role="tab" id="headingSeven">
            <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
                <h2>Bites and Stings</h2>
            </a>
        </div>
        <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
            <div class="panel-body">
                <a href="health-resources/mosquitoes.html"><h4>Mosquitoes</h4></a>
                <a href="health-resources/ticks.html"><h4>Ticks</h4></a>
                <a href="#"><h4>Bees and Wasps</h4></a>
                <a href="#"><h4>Animal Bites</h4></a>
            </div>
        </div>
    </div>


    <div class="panel panel-default col-md-4">
        <div class="panel-heading pet-health" role="tab" id="headingTwo">
            <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                <h2>Pet Health and Safety</h2>
            </a>
        </div>
        <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
            <div class="panel-body">
                <a href="#"><h4>Pet Allergies</h4></a>
                <a href="#"><h4>Toxic Plants</h4></a>
                <a href="#"><h4>Pet and Wildlife Encounters</h4></a>
            </div>
        </div>
    </div>


    <div class="panel panel-default col-md-4">
        <div class="panel-heading livestock-health" role="tab" id="headingThree">
            <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
                <h2>Livestock Health and Safety</h2>
            </a>
        </div>
        <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
            <div class="panel-body">
                <a href="#"><h4>This is a Topic</h4></a>
                <a href="#"><h4>Topic 2</h4></a>
                <a href="#"><h4>Another Topic</h4></a>
            </div>
        </div>
    </div>
</div>
</div>

<div class="row">
<div class="panel-group" id="accordion2" role="tablist" aria-multiselectable="true">
    <div class="panel panel-default col-md-4">
        <div class="panel-heading zoonotic" role="tab" id="headingFour">
            <a role="button" data-toggle="collapse" data-parent="#accordion2" href="#collapseFour" aria-expanded="true" aria-controls="collapseFour">
                <h2>Zoonotic Diseases</h2>
            </a>
        </div>
        <div id="collapseFour" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingFour">
            <div class="panel-body">
                <a href="#"><h4>Bacterial</h4></a>
                <a href="#"><h4>Fungal</h4></a>
                <a href="#"><h4>Parasitic</h4></a>
                <a href="#"><h4>Prionotic</h4></a>
                <a href="#"><h4>Rickettsial</h4></a>
                <a href="#"><h4>Spirochetes</h4></a>
                <a href="#"><h4>Viral</h4></a>
            </div>
        </div>
    </div>


    <div class="panel panel-default col-md-4">
        <div class="panel-heading allergies" role="tab" id="headingFive">
            <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion2" href="#collapseFive" aria-expanded="false" aria-controls="collapseFive">
                <h2>Allergies</h2>
            </a>
        </div>
        <div id="collapseFive" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingFive">
            <div class="panel-body">
                <a href="#"><h4>Animal Allergies</h4></a>
                <a href="#"><h4>Environmental Allergies</h4></a>
            </div>
        </div>
    </div>


    <div class="panel panel-default col-md-4">
        <div class="panel-heading environment-health" role="tab" id="headingSix">
            <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion2" href="#collapseSix" aria-expanded="false" aria-controls="collapseSix">
                <h2>Environmental Health</h2>
            </a>
        </div>
        <div id="collapseSix" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingSix">
            <div class="panel-body">
                <a href="#"><h4>This is a Topic</h4></a>
                <a href="#"><h4>Topic 2</h4></a>
                <a href="#"><h4>Another Topic</h4></a>
            </div>
        </div>
    </div>


</div><!--/collapse-->
</div>
</div>

1 个答案:

答案 0 :(得分:0)

要拥有多个手风琴,但只有一个同时打开,您只需要组合data-parent。所以我为你做了一个codepen看到它正常工作。

我刚将所有data-parent更改为data-parent="#accordion,#accordion2"

    <div class="row">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">

    <div class="panel panel-default col-md-4">
        <div class="panel-heading  bites-stings" role="tab" id="headingSeven">
            <a role="button" data-toggle="collapse" data-parent="#accordion,#accordion2" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
                <h2>Bites and Stings</h2>
            </a>
        </div>
        <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
            <div class="panel-body">
                <a href="health-resources/mosquitoes.html"><h4>Mosquitoes</h4></a>
                <a href="health-resources/ticks.html"><h4>Ticks</h4></a>
                <a href="#"><h4>Bees and Wasps</h4></a>
                <a href="#"><h4>Animal Bites</h4></a>
            </div>
        </div>
    </div>


    <div class="panel panel-default col-md-4">
        <div class="panel-heading pet-health" role="tab" id="headingTwo">
            <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion,#accordion2" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                <h2>Pet Health and Safety</h2>
            </a>
        </div>
        <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
            <div class="panel-body">
                <a href="#"><h4>Pet Allergies</h4></a>
                <a href="#"><h4>Toxic Plants</h4></a>
                <a href="#"><h4>Pet and Wildlife Encounters</h4></a>
            </div>
        </div>
    </div>


    <div class="panel panel-default col-md-4">
        <div class="panel-heading livestock-health" role="tab" id="headingThree">
            <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion,#accordion2" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
                <h2>Livestock Health and Safety</h2>
            </a>
        </div>
        <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
            <div class="panel-body">
                <a href="#"><h4>This is a Topic</h4></a>
                <a href="#"><h4>Topic 2</h4></a>
                <a href="#"><h4>Another Topic</h4></a>
            </div>
        </div>
    </div>
</div>
</div>

<div class="row">
<div class="panel-group" id="accordion2" role="tablist" aria-multiselectable="true">
    <div class="panel panel-default col-md-4">
        <div class="panel-heading zoonotic" role="tab" id="headingFour">
            <a role="button" data-toggle="collapse" data-parent="#accordion,#accordion2" href="#collapseFour" aria-expanded="true" aria-controls="collapseFour">
                <h2>Zoonotic Diseases</h2>
            </a>
        </div>
        <div id="collapseFour" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingFour">
            <div class="panel-body">
                <a href="#"><h4>Bacterial</h4></a>
                <a href="#"><h4>Fungal</h4></a>
                <a href="#"><h4>Parasitic</h4></a>
                <a href="#"><h4>Prionotic</h4></a>
                <a href="#"><h4>Rickettsial</h4></a>
                <a href="#"><h4>Spirochetes</h4></a>
                <a href="#"><h4>Viral</h4></a>
            </div>
        </div>
    </div>


    <div class="panel panel-default col-md-4">
        <div class="panel-heading allergies" role="tab" id="headingFive">
            <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion,#accordion2" href="#collapseFive" aria-expanded="false" aria-controls="collapseFive">
                <h2>Allergies</h2>
            </a>
        </div>
        <div id="collapseFive" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingFive">
            <div class="panel-body">
                <a href="#"><h4>Animal Allergies</h4></a>
                <a href="#"><h4>Environmental Allergies</h4></a>
            </div>
        </div>
    </div>


    <div class="panel panel-default col-md-4">
        <div class="panel-heading environment-health" role="tab" id="headingSix">
            <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion,#accordion2" href="#collapseSix" aria-expanded="false" aria-controls="collapseSix">
                <h2>Environmental Health</h2>
            </a>
        </div>
        <div id="collapseSix" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingSix">
            <div class="panel-body">
                <a href="#"><h4>This is a Topic</h4></a>
                <a href="#"><h4>Topic 2</h4></a>
                <a href="#"><h4>Another Topic</h4></a>
            </div>
        </div>
    </div>


</div><!--/collapse-->

</div>