如何在按下下一个折叠折叠选项卡后折叠手风琴选项卡



我正在尝试做一个FAQ页面。这是我的代码。我正在使用 Twitter Bootstrap 2.3.2

<!-- Snippet Code -->
<div class="accordion" id="accordion2">
    <!-- Each item should be enclosed inside the class "accordion-group". Note down the below markup. -->
    <div class="accordion-group">
        <div class="accordion-heading"> <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" href="#1">
                              <!-- Title. Don't forget the <i> tag. -->
                             <h5><i class="icon-plus"></i> What is Lorem Ipsum?</h5>
                           </a>
        </div>
        <div id="1" class="accordion-body collapse" style="height: 0px;">
            <div class="accordion-inner">
                <!-- Para -->
                <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            </div>
        </div>
    </div>
    <div class="accordion-group">
        <div class="accordion-heading"> <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" href="#2">
                              <!-- Title. Don't forget the <i> tag. -->
                             <h5><i class="icon-plus"></i> Where does it come from?</h5>
                           </a>
        </div>
        <div id="2" class="accordion-body collapse" style="height: 0px;">
            <div class="accordion-inner">
                <!-- Para -->
                <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
            </div>
        </div>
    </div>
    <div class="accordion-group">
        <div class="accordion-heading"> <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" href="#3">
                              <!-- Title. Don't forget the <i> tag. -->
                             <h5><i class="icon-plus"></i> Why do we use it?</h5>
                           </a>
        </div>
        <div id="3" class="accordion-body collapse" style="height: 0px;">
            <div class="accordion-inner">
                <!-- Para -->
                <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
            </div>
        </div>
    </div>
    <div class="accordion-group">
        <div class="accordion-heading"> <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" href="#4">
                              <!-- Title. Don't forget the <i> tag. -->
                             <h5><i class="icon-plus"></i> Where can I get Some?</h5>
                           </a>
        </div>
        <div id="4" class="accordion-body collapse" style="height: 0px;">
            <div class="accordion-inner">
                <!-- Para -->
                <p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p>
            </div>
        </div>
    </div>
</div>
<!-- Snipet Code end -->

演示 : http://jsfiddle.net/vVCp9/

我的问题是当我单击此手风琴菜单中的项目时,然后如果我单击下一个,第一个选项卡将保持折叠状态.所以它必须崩溃。

我想一定是这样,但我无法成功。

$(function() {
    $('.accordion').on('show', function (e) {
         $(e.target).prev('.accordion-heading').find('.accordion-toggle').addClass('active');
    });
    $('.accordion').on('hide', function (e) {
        $(this).find('.accordion-toggle').not($(e.target)).removeClass('active');
    });
});

只需在 data-parent 中设置一个有效的选择器,当一个项目展开时,它将折叠作为 data-parent 指定的元素的后代的所有其他可折叠元素。

因此,只需在所有手风琴标题div 中设置 data-parent="#accordion2"。

最新更新