2017-09-26 128 views
0

我正在使用Bootstrap製作手風琴,我想動態添加面板。我有那部分工作。看到這個fiddle使用Javascript打開Bootstrap手風琴面板

我可以正確添加上飛板和手風琴功能(當你點擊一個面板,它縮短了,也關閉任何先前打開的面板

我想添加一個附加功能在添加新面板時,expandLast()函數自動打開最後添加的面板(結果應該關閉之前打開的面板)。但它不但不會那樣做,還會打破手風琴的功能;除非再次點擊,否則所有面板都會保持打開狀態。看到下面的代碼:

var count = 1; 

$('#myBtn').click(function() { 

    var parent = document.getElementById("accordion"); 
    var wrapper = document.createElement('div'); 
    wrapper.className = "panel panel-default"; 


    var title = document.createElement('div'); 
    title.className = "panel-heading"; 
    title.setAttribute("id", "cartItemTitle"); 
    title.setAttribute("data-toggle", "collapse"); 
    title.setAttribute("data-target", "#collapsible-" + count); 
    title.setAttribute("data-parent", "#accordion"); 
    title.innerHTML = "panel: " + count; 

    var body = document.createElement('div'); 
    body.setAttribute("id", "collapsible-" + count); 
    body.className = "panel-collapse collapse"; 
    //body.className="panel-body"; 
    body.innerHTML = "Lorem ipsum dolor sit amet, habeo novum possim in duo, solet aperiam postulant at eam. Te dolore ullamcorper vim. Semper officiis ad vix. Maluisset aliquando consectetuer ne pro. Mollis docendi at mei, errem dolorem voluptaria sed ea."; 

    wrapper.appendChild(title); 
    wrapper.appendChild(body); 

    parent.appendChild(wrapper); 

    count = count + 1; 

    expandLast(); 
}); 

function expandLast() 
{ 
    var allItems=document.getElementsByClassName("collapse"); 
    var lastItem=allItems[allItems.length-1]; 
    var lastItemSelector="#"+lastItem.getAttribute("id"); 
    $(lastItemSelector).collapse(); 
} 

任何想法是什麼原因造成這種情況? - 感謝

回答

0

我重構了創建面板以包含容器和錨定標籤以使其具有適當面板結構的JavaScript。您可以根據需要進行修改。

$parent.find('> div:last-of-type a').trigger('click');

這裏有一個工作示例:

var count = 1; 
 

 
$('#myBtn').click(function() { 
 

 
    var $parent = $('#accordion'); 
 
    var $wrapper = $('<div>', { class: 'panel panel-default' }); 
 
    
 
    var $panelHeading = $('<div>', { 
 
    id: 'heading' + count, 
 
    class: 'panel-heading', 
 
    role: 'tab' 
 
    }); 
 

 
    var $titleLink = $('<h4>', { 
 
    class: 'panel-title' 
 
    }).append($('<a>', { 
 
    role: 'button', 
 
    class: 'collapsed', 
 
    'data-toggle': 'collapse', 
 
    'data-parent': '#accordion', 
 
    'href': '#collapsible-' + count, 
 
    'aria-expanded': 'true', 
 
    'aria-controls': 'collapsible-' + count 
 
    }).text('New Panel ' + count)); 
 

 
    var $panelBody = $('<div class="panel-body">') 
 
    .text("Lorem ipsum dolor sit amet, habeo novum possim in duo, solet aperiam postulant at eam. Te dolore ullamcorper vim. Semper officiis ad vix. Maluisset aliquando consectetuer ne pro. Mollis docendi at mei, errem dolorem voluptaria sed ea."); 
 
    
 
    var $panelContainer = $('<div>', { 
 
    id: 'collapsible-'+count, 
 
    class: 'panel-collapse collapse', 
 
    role: 'tabpanel', 
 
    'aria-labelledby': 'heading'+count 
 
    }); 
 

 
    $wrapper.append($panelHeading.append($titleLink)); 
 
    $wrapper.append($panelContainer.append($panelBody)); 
 
    $parent.append($wrapper); 
 
    $parent.find('> div:last-of-type a').trigger('click'); 
 

 
    count++; 
 
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 

 
<a id="myBtn" class="btn btn-primary">Add Item</a> 
 

 
<hr> 
 

 
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true"> 
 
    <div class="panel panel-default"> 
 
    <div class="panel-heading" role="tab" id="headingOne"> 
 
     <h4 class="panel-title"> 
 
     <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> 
 
      Collapsible Group Item #1 
 
     </a> 
 
     </h4> 
 
    </div> 
 
    <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne"> 
 
     <div class="panel-body"> 
 
     Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. 
 
     </div> 
 
    </div> 
 
    </div> 
 
    <div class="panel panel-default"> 
 
    <div class="panel-heading" role="tab" id="headingTwo"> 
 
     <h4 class="panel-title"> 
 
     <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"> 
 
      Collapsible Group Item #2 
 
     </a> 
 
     </h4> 
 
    </div> 
 
    <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo"> 
 
     <div class="panel-body"> 
 
     Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. 
 
     </div> 
 
    </div> 
 
    </div> 
 
    <div class="panel panel-default"> 
 
    <div class="panel-heading" role="tab" id="headingThree"> 
 
     <h4 class="panel-title"> 
 
     <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree"> 
 
      Collapsible Group Item #3 
 
     </a> 
 
     </h4> 
 
    </div> 
 
    <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree"> 
 
     <div class="panel-body"> 
 
     Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

,你可能想嘗試通過使用類似下面的觸發新的面板的主要部分