根据活跃的孩子

时间:2017-04-18 01:57:21

标签: jquery css jquery-ui jquery-ui-accordion

顶级手风琴部分是“活跃的”。我想实现一个基于子元素变为活动的菜单。例如;正如您所看到的,“仪表板”设置为活动状态,具有“活动”类,因为它是当前页面。

如果子元素具有“活动”类,如何激活手风琴? (如网站上的下拉菜单)

Navigation Accordion

代码:

<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>

1 个答案:

答案 0 :(得分:2)

您希望为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作为类的链接的Panel将会打开。

工作示例:https://jsfiddle.net/Twisty/76jrtx62/