让两个Bootstrap Accordions在标签内工作

时间:2015-04-17 15:33:40

标签: javascript jquery css twitter-bootstrap jquery-ui

我在尝试让两个Bootstrap Accordions在同一页面上的不同标签中工作时遇到问题。

基本上,当我点击第一个手风琴时,所有内容都会正常工作,内容会按预期折叠和展开。

然而,当谈到第二个手风琴时,点击链接没有任何影响,但它确实将我的CSS样式应用于元素。

我的问题是如何根据手风琴#1的预期行为使手风琴折叠和扩展?

<div role="tabpanel">

  <!-- Nav tabs -->
  <ul class="nav nav-tabs" role="tablist">
    <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>
    <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
    <li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li>
    <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
  </ul>

  <!-- Tab panes -->
  <div class="tab-content">
    <div role="tabpanel" class="tab-pane active" id="home">
        <!-- ACCORDION NO.1 -->
        <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
            <div class="panel panel-default">
                <div class="panel-heading edu1 active-state" role="tab" id="headingOne">
                    <h4 class="panel-title">
                        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">HEADING 1</a></h4>
                </div>
                <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
                    <div class="panel-body">
                        ***** SOME TEXT *****
                    </div>
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading edu1" role="tab" id="headingTwo">
                    <h4 class="panel-title">
                        <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">HEADING 2</a></h4>
                </div>
                <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
                    <div class="panel-body">
                        ***** SOME TEXT *****
                    </div>
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading edu1" role="tab" id="headingThree">
                    <h4 class="panel-title">
                        <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">HEADING 3</a>
                    </h4>
                </div>
                <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
                    <div class="panel-body">
                        ***** SOME TEXT *****
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div role="tabpanel" class="tab-pane" id="profile">
    ***** SOME OTHER CONTENT *****
    </div>
    <div role="tabpanel" class="tab-pane" id="messages">
        <!-- ACCORDION NO.2 -->
        <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
            <div class="panel panel-default">
                <div class="panel-heading prev-emp activestate" role="tab" id="headingOne">
                    <h4 class="panel-title">
                        <a data-toggle="collapse" data-parent="#accordion-2" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">HEADING 1</a>
                    </h4>
                </div>

                <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
                    <div class="container panel-body">
                        ***** SOME TEXT *****
                    </div>
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-heading prev-emp" role="tab" id="headingTwo">
                    <h4 class="panel-title">
                        <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">HEADING 2</a>
                    </h4>
                </div>
                <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
                    <div class="container panel-body">
                        ***** SOME TEXT *****
                    </div>
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-heading prev-emp" role="tab" id="headingThree">
                    <h4 class="panel-title">
                        <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">HEADING 3</a>
                    </h4>
                </div>
                <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
                    <div class="container panel-body">
                        ***** SOME TEXT *****
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div role="tabpanel" class="tab-pane" id="settings">
        ***** SOME OTHER CONTENT *****
    </div>
  </div>
</div>

这是我正在使用的jQuery;

$(function() {
  $('.panel-heading').click(function() {
    if ($(this).hasClass('activestate')) {
      $(this).removeClass('activestate');
    } else {
      $('.panel-heading').removeClass('activestate');
      $(this).addClass('activestate');
    }
 });
});

我在这里用tabal1中的accordion1和tab 3中的accordion 2复制了我的问题:http://jsfiddle.net/4yk7yzo9/1/

我是jQuery的no0b所以请好好玩,并提前感谢任何正确方向的指针

3 个答案:

答案 0 :(得分:1)

此重复的ID是问题

 <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
                        <div class="panel-body">
                            ***** SOME TEXT *****
                        </div>
                    </div>

答案 1 :(得分:1)

你只是为手风琴重复相同的ID。而且,有些data-parents混淆了,Here is your Updated Fiddle

请参阅下面第二部手风琴的一些变化:

    <div class="panel-group" id="accordion-2" role="tablist" aria-multiselectable="true">
        <div class="panel panel-default">
            <div class="panel-heading prev-emp activestate" role="tab" id="headingOne">
                <h4 class="panel-title">
                    <a data-toggle="collapse" data-parent="#accordion-2" href="#collapseFour" aria-expanded="true" aria-controls="collapseFour">HEADING 1</a>
                </h4>
            </div>

            <div id="collapseFour" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
                <div class="container panel-body">
                    ***** SOME TEXT *****
                </div>
            </div>
        </div>

        <div class="panel panel-default">
            <div class="panel-heading prev-emp" role="tab" id="headingTwo">
                <h4 class="panel-title">
                    <a class="collapsed" data-toggle="collapse" data-parent="#accordion-2" href="#collapseFive" aria-expanded="false" aria-controls="collapseFive">HEADING 2</a>
                </h4>
            </div>
            <div id="collapseFive" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
                <div class="container panel-body">
                    ***** SOME TEXT *****
                </div>
            </div>
        </div>

    <!-- Start of Previous Employment Tab #3 -->
        <div class="panel panel-default">
            <div class="panel-heading prev-emp" role="tab" id="headingThree">
                <h4 class="panel-title">
                    <a class="collapsed" data-toggle="collapse" data-parent="#accordion-2" href="#collapseSix" aria-expanded="false" aria-controls="collapseSix">HEADING 3</a>
                </h4>
            </div>
            <div id="collapseSix" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
                <div class="container panel-body">
                    ***** SOME TEXT *****
                </div>
            </div>
        </div>
    </div>

答案 2 :(得分:1)

您对每个标签一遍又一遍地使用相同的ID。这就是你的3手风琴套装具有相似的id属性。

这是工作小提琴,更新你的小提琴。 :) https://jsfiddle.net/Lq26j75z/