波本手风琴单击时单击一次展开/折叠

时间:2017-12-02 20:01:05

标签: javascript jquery bourbon

您好,我是新来的,我想知道是否有人可以提供帮助。我已经实现了bourbon.io的手风琴,但我无法通过一次点击打开并折叠。

它打开,但随后保持打开状态。

$('.js-accordion-trigger').bind('click', function(e){
  jQuery(this).parent().find('.submenu').slideToggle('fast');  // apply the toggle to the ul
  jQuery(this).parent().toggleClass('is-expanded');
  e.preventDefault();
});

JSFiddle Demo

1 个答案:

答案 0 :(得分:0)

如果这是您想要的话,请参见下文



$('.js-accordion-trigger').on('click', function(e) {
  $('ul.submenu').not($(this).parent().find('ul.submenu')).slideUp('fast');
  $(this).parent().find('.submenu').toggleClass('is-expanded').slideToggle('fast');
  e.preventDefault();
});

ul.submenu {
  display: none;
}

.is-expanded {
  display: block;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="accordion">
  <li>
    <a href="javascript:void(0)" class="js-accordion-trigger">Accordion Item</a>
    <ul class="submenu">
      <li>
        <a href="javascript:void(0)">Sub Item 1</a>
      </li>
      <li>
        <a href="javascript:void(0)">Sub Item 2</a>
      </li>
    </ul>
  </li>
  <li>
    <a href="javascript:void(0)" class="js-accordion-trigger">Another Item</a>
    <ul class="submenu">
      <li>
        <a href="javascript:void(0)">Sub Item 1</a>
      </li>
      <li>
        <a href="javascript:void(0)">Sub Item 2</a>
      </li>
    </ul>
  </li>
</ul>
&#13;
&#13;
&#13;