Ion*_*zău 12 html javascript jquery twitter-bootstrap
我使用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
Run Code Online (Sandbox Code Playgroud)
问题是如果打开Panel 2,则打开动态面板(从面板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());
});
Run Code Online (Sandbox Code Playgroud)
Anu*_*ngh 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());
});
Run Code Online (Sandbox Code Playgroud)