12

我使用Bootstrap 3手风琴,我需要在其中添加动态面板。我有这样的事情:

+------------------+
| Panel 1 (closed) |
+------------------+
| Panel 2 (opened) | <-- This is the template panel
| CONTENT          |
+------------------+
| Dynamic panels   | <-- All dynamic panels must be closed, not opened
+------------------+     after they are added

问题是如果面板 2被打开,动态面板(从面板 2克隆)被打开。如果面板 2关闭,则动态面板将关闭。

我希望关闭所有动态面板,并且只有在单击它们的标题时才会打开它们(例如在 Bootstrap 示例中)。我该如何解决?

这是我的 jQuery 代码。

var $template = $(".template");

var hash = 2;
$(".btn-add-panel").on("click", function () {
    var $newPanel = $template.clone();
    // I thought that .in class makes the panel to be opened
    $newPanel.find(".collapse").removeClass("in");
    $newPanel.find(".accordion-toggle").attr("href",  "#" + (++hash))
             .text("Dynamic panel #" + hash);
    $newPanel.find(".panel-collapse").attr("id", hash);
    $("#accordion").append($newPanel.fadeIn());
});

JSFiddle

4

1 回答 1

19

我刚刚addClass("collapse")在这一行添加了:

$newPanel.find(".panel-collapse").attr("id", hash).addClass("collapse").removeClass("in");

var $template = $(".template");

var hash = 2;
$(".btn-add-panel").on("click", function () {
    var $newPanel = $template.clone();
    $newPanel.find(".collapse").removeClass("in");
    $newPanel.find(".accordion-toggle").attr("href",  "#" + (++hash))
             .text("Dynamic panel #" + hash);
    $newPanel.find(".panel-collapse")
             .attr("id", hash)
             .addClass("collapse")
             .removeClass("in");
    $("#accordion").append($newPanel.fadeIn());
});

小提琴演示

于 2013-09-16T10:59:08.473 回答