Jquery 如何在按下下一个手风琴标签后打开手风琴标签?

Jquery 如何在按下下一个手风琴标签后打开手风琴标签?,jquery,html,css,twitter-bootstrap,Jquery,Html,Css,Twitter Bootstrap,我正在尝试做一个常见问题页面。 这是我的密码我正在使用Twitter引导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="acc

我正在尝试做一个常见问题页面。 这是我的密码我正在使用Twitter引导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 -->

只要在DataParent中设置一个有效的选择器,当一个项被展开时,它将每隔一个可折叠的元素折叠一次,该元素是DataParent指定的元素的后代


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

您能从JSFIDLE中创建一个吗?虽然JSFIDLE很好,但最好在这里包含解决方案的代码
$(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');
    });

});