在手风琴中折叠动态添加的选项卡
Posted
技术标签:
【中文标题】在手风琴中折叠动态添加的选项卡【英文标题】:Collapsing dynamically added tabs in accordion 【发布时间】:2018-01-01 01:19:16 【问题描述】:这是一个示例。
$(function()
var icons =
header: "ui-icon-circle-arrow-e",
headerSelected: "ui-icon-circle-arrow-s"
;
$( "#accordion" ).accordion(
icons: icons,
collapsible: true
);
$( "#header1" ).click(function(e) return false;
$( "#accordion" ).accordion( "option", "icons", false );
);
);
$('#accordion .accClicked')
.off('click')
.click(function()
$(this).next().toggle('fast');
);
$('#add').click(function()
var $tab="<h3 id='header3' class='accClicked'><a href='#'>Section 3</a></h3><div>Bonjour ***</div>";
$('#accordion').append($tab);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div id="accordion">
<h3 id="header1" class="accClicked"><a href="#">Section 1</a></h3>
<div> Good Morning ***</div>
<h3 id="header2" class="accClicked"><a href="#">Section 2</a></h3>
<div>Buongiorno ***</div>
<h3 id="header3" class="accClicked"><a href="#">Section 3</a></h3>
<div>Bonjour ***</div>
</div>
<button id="add">
Add tab
</button>
http://jsfiddle.net/xso14hct/
我可以添加一个新标签,但我不能折叠它。我搜索并发现我应该以某种方式更新 DOM,但我只是没有找到我该怎么做。
我可以简单地使用 jquery-ui 手风琴,但问题是,当您打开另一个选项卡时,它会自动关闭打开的选项卡,但我不需要。这是它的链接:JQuery-UI Accordion
【问题讨论】:
【参考方案1】:您可以将点击重写变成一个函数,并为您添加的每个选项卡再次运行它:
$(function()
var icons =
header: "ui-icon-circle-arrow-e",
headerSelected: "ui-icon-circle-arrow-s"
;
$("#accordion").accordion(
icons: icons,
collapsible: true
);
$("#header1").click(function(e)
return false;
$("#accordion").accordion("option", "icons", false);
);
);
// from here we change to:
function resetTabClick()
$('#accordion .accClicked')
.off('click').click(function()
$(this).next().toggle('fast');
);
resetTabClick();
$('#add').click(function()
var $tab="<h3 id='header3' class='accClicked'><a href='#'>Section 3</a></h3><div>Bonjour ***</div>";
$('#accordion').append($tab);
resetTabClick();
);
【讨论】:
【参考方案2】:你很亲密。您只需要将选项卡的回调函数包装在一个函数中,以便在向 DOM 添加新选项卡时调用它。
此外,如果您希望新标签页最初关闭,请添加样式 display: none;
。
$(function()
var icons =
header: "ui-icon-circle-arrow-e",
headerSelected: "ui-icon-circle-arrow-s"
;
$("#accordion").accordion(
icons: icons,
collapsible: true
);
$("#header1").click(function(e)
return false;
$("#accordion").accordion("option", "icons", false);
);
);
function makeNewTab()
$('#accordion .accClicked')
.off('click').click(function()
$(this).next().toggle('fast');
);
$('#add').click(function()
var $tab="<h3 id='header3' class='accClicked'><a href='#'>Section 3</a></h3><div style='display:none;'>Bonjour ***</div>";
$('#accordion').append($tab);
makeNewTab();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div id="accordion">
<h3 id="header1" class="accClicked"><a href="#">Section 1</a></h3>
<div> Good Morning ***</div>
<h3 id="header2" class="accClicked"><a href="#">Section 2</a></h3>
<div>Buongiorno ***</div>
<h3 id="header3" class="accClicked"><a href="#">Section 3</a></h3>
<div>Bonjour ***</div>
</div>
<button id="add">
Add tab
</button>
【讨论】:
【参考方案3】:您需要将处理程序分配给新选项卡。检查这个
$(function()
var icons =
header: "ui-icon-circle-arrow-e",
headerSelected: "ui-icon-circle-arrow-s"
;
$("#accordion").accordion(
icons: icons,
collapsible: true
);
$("#header1").click(function(e)
return false;
$("#accordion").accordion("option", "icons", false);
);
);
$('#accordion .accClicked')
.off('click')
.click(function()
$(this).next().toggle('fast');
);
var i = 4;
$('#add').click(function()
var $tab = "<h3 id='header" + i + "' class='accClicked'><a href='#'>Section " + i + "</a></h3><div>Bonjour ***</div>";
$('#accordion').append($tab);
$('#accordion .accClicked')
.off('click')
.click(function()
$(this).next().toggle('fast');
);
i++;
);
【讨论】:
以上是关于在手风琴中折叠动态添加的选项卡的主要内容,如果未能解决你的问题,请参考以下文章