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();
}
任何想法是什麼原因造成這種情況? - 感謝