2017-09-26 161 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>

,你可能想尝试通过使用类似下面的触发新的面板的主要部分