我使用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
问题是,如果打开 Panel 2 ,则会打开动态面板(从 Panel 2 克隆)。如果 Panel 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());
});
答案 0 :(得分:18)
我刚在这一行添加了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());
});