顶部手风琴部分处于“活动状态”。我想实现一个基于子元素变为活动菜单的菜单。例如;如您所见,“仪表板”已设置为“活动”类,因为它是当前页面。
如果子元素具有“ active”类,如何使手风琴活动? (如网站上的下拉菜单)
码:
<nav id="side-nav">
<div class="accordion ui-accordion ui-widget ui-helper-reset" role="tablist">
<h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-accordion-header-active ui-state-active ui-corner-top ui-accordion-icons" role="tab" id="ui-accordion-1-header-0" aria-controls="ui-accordion-1-panel-0" aria-selected="true" tabindex="0">
<span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-s"></span>Core Application
</h3>
<div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" id="ui-accordion-1-panel-0" aria-labelledby="ui-accordion-1-header-0" role="tabpanel" aria-expanded="true" aria-hidden="false" style="display: block; height: 360px;">
<a class="active" href="">Dashboard</a>
<a href="customers">Customers</a>
<a href="staff">Staff</a>
<a href="tours">Tours</a>
<a href="users">Users</a>
<a href="settings">Settings</a>
</div>
<h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-accordion-icons" role="tab" id="ui-accordion-1-header-1" aria-controls="ui-accordion-1-panel-1" aria-selected="false" tabindex="-1">
<span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-e"></span>Another menu
</h3>
<div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" id="ui-accordion-1-panel-1" aria-labelledby="ui-accordion-1-header-1" role="tabpanel" aria-expanded="false" aria-hidden="true" style="display: none; height: 360px;">
<a href="medication/records"><b class=""></b>Medical Records</a>
</div>
</div>
<script> $(".accordion").accordion();</script>
</nav>
最佳答案
您要为Accordian设置active
选项。查看更多:http://api.jqueryui.com/accordion/#option-active
可以这样完成:
<script>
$(function() {
var activeTab = 0;
$(".accordion div").each(function(i, el) {
if ($(el).find(".active").length) {
activeTab = i;
}
});
$(".accordion").accordion({
active: activeTab
});
});
</script>
当页面加载时,具有链接的面板(以
active
作为类)将打开。工作示例:https://jsfiddle.net/Twisty/76jrtx62/
关于jquery - 根据活跃的 child 将 Accordion 设为活跃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43462330/