2017-02-14 130 views
0

我知道這裏有類似的帖子,但我找不到答案。Bootstrap手風琴:單擊另一個時不會自動摺疊

我有一個自定義佈局,但我想嘗試添加手風琴效果,但我似乎無法弄清楚爲什麼,但由於某種原因,一切正常,除非他們不會自動關閉,一旦另一個是點擊。

這裏是我的手風琴代碼:

<aside class="col-lg-4"> 
    <div class="list-group"> 
    <ul class="product-sorting"> 
     <div id="accordion" role="tablist" aria-multiselectable="true" class="panel panel-group"> 
     <li><a href="#" data-group="all" class="list-group-item active">All Products</a></li> 
     <li> 
     <a data-group="IQF" class="list-group-item" data-toggle="collapse" data-parent="#accordion" href="#collapse1" aria-expanded="false" aria-controls="collapse1">IQF Fruits (Individually Quick Frozen) 
     </a> 
     <div id="collapse1" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading1"> 
      <div class="panel-body"> 
      <p>Juice that is directly squeezed from a fruit/ vegetable. 100% juice contains no added sugar or preservatives.</p> 
      </div> 
     </div> 
     </li> 
     <li> 
     <a data-group="Fruit-Concentrate" class="list-group-item" data-toggle="collapse" data-parent="#accordion" href="#collapse2" aria-expanded="false" aria-controls="collapse2"> 
     Fruit Juice Concentrate 
     </a> 
     <div id="collapse2" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading2"> 
      <div class="panel-body"> 
      <p>Juice that is directly squeezed from a fruit/ vegetable. 100% juice contains no added sugar or preservatives.</p> 
      </div> 
     </div> 
     </li> 
     <li> 
     <a data-group="Puree-pulp" class="list-group-item" data-toggle="collapse" data-parent="#accordion" href="#collapse3" aria-expanded="false" aria-controls="collapse3"> 
     Fruit Puree/ Pulp 
     </a> 
     <div id="collapse3" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading3"> 
      <div class="panel-body"> 
      <p>Juice that is directly squeezed from a fruit/ vegetable. 100% juice contains no added sugar or preservatives.</p> 
      </div> 
     </div> 
     </li> 
     <li> 
     <a data-group="Puree-Concentrate" class="list-group-item" data-toggle="collapse" data-parent="#accordion" href="#collapse4" aria-expanded="false" aria-controls="collapse4"> 
     Fruit Puree Concentrate 
     </a> 
     <div id="collapse4" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading4"> 
      <div class="panel-body"> 
      <p>Juice that is directly squeezed from a fruit/ vegetable. 100% juice contains no added sugar or preservatives.</p> 
      </div> 
     </div> 
     </li> 
     <li> 
     <a data-group="NFC" class="list-group-item" data-toggle="collapse" data-parent="#accordion" href="#collapse5" aria-expanded="false" aria-controls="collapse5"> 
     NFC 
     </a> 
     <div id="collapse5" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading5"> 
      <div class="panel-body"> 
      <p>Juice that is directly squeezed from a fruit/ vegetable. 100% juice contains no added sugar or preservatives.</p> 
      </div> 
     </div> 
     </li> 
     <li> 
     <a data-group="Organic" class="list-group-item" data-toggle="collapse" data-parent="#accordion" href="#collapse6" aria-expanded="false" aria-controls="collapse6"> 
     Organic 
     </a> 
      <div id="collapse6" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading6"> 
      <div class="panel-body"> 
      <p>Juice that is directly squeezed from a fruit/ vegetable. 100% juice contains no added sugar or preservatives.</p> 
      </div> 
     </div> 
     </li> 
     <li> 
     <a data-group="Dried" class="list-group-item" data-toggle="collapse" data-parent="#accordion" href="#collapse7" aria-expanded="false" aria-controls="collapse7">Dried Fruit/ Sweetened Dried Fruit 
     </a> 
     <div id="collapse7" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading7"> 
      <div class="panel-body"> 
      <p>Juice that is directly squeezed from a fruit/ vegetable. 100% juice contains no added sugar or preservatives.</p> 
      </div> 
     </div> 
     </li> 
     </div> 
    </ul> 
    </div> 
    </aside> 

這是什麼樣子:

文本「汁直接從水果/蔬菜榨100%純果汁不含添加糖或防腐劑「僅僅是虛擬文本,對於所有面板都是一樣的,所以不要讓你迷惑。

Default state all cool

Fist Accordion click - still all good

As soon as you click another accordion that one expands whilst the first one stays expanded.

回答

1

使用<div class="accordion-group">

,並分配data-parent="#parentdivid"到每個手風琴。

示例代碼段:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 
 
<h3>ACCORDION COLLAPSE</h3> 
 
<div class="accordion" id="myAccordion"> 
 
    <div class="panel"> 
 
     <button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#collapsible-1" data-parent="#myAccordion">Question 1?</button> 
 
     <div id="collapsible-1" class="collapse"> 
 
      .. 
 
     </div> 
 
    </div> 
 
    <div class="panel"> 
 
     <button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#collapsible-2" data-parent="#myAccordion">Question 2?</button> 
 
     <div id="collapsible-2" class="collapse"> 
 
      .. 
 
     </div> 
 
    </div> 
 
    <div class="panel"> 
 
     <button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#collapsible-3" data-parent="#myAccordion">Question 3?</button> 
 
     <div id="collapsible-3" class="collapse"> 
 
      ... 
 
     </div> 
 
    </div> 
 
</div>

+0

我想,現在在我的代碼,不能正常工作。 - 您還需要哪些其他信息?我可以發佈更多,如果你想。 –

+0

謝謝普拉韋什,這對我來說非常完美! – otrebla