打开时关闭所有 jQuery 手风琴

问题描述

我在不同位置的页面上使用多个单独的手风琴。 如果我打开一个手风琴选项卡,所有其他“独立”的手风琴都应该关闭

正如你在这里看到的: jsfiddle

example at jsfiddle

如果我打开“SECOND Accordion - Secion x”,“First Accordion”中的所有部分都应该折叠起来。我需要通用代码,所以 ALL OTHER 应该关闭

提前致谢!

解决方法

试试下面的可运行示例。

我添加了第三个手风琴来查看行为。 3 个手风琴中只有一个会处于活动状态,其他手风琴会崩溃。

xcrun scntool --convert fileIn.scn --output fileOut.dae --format dae
$( function() {
    $( ".accordion" ).accordion({
      heightStyle: "content",collapsible: true,active: false
    });

    $('.accordion').click(function() {
      // Get all the accordions
      let accordions = $('.accordion');

      // Remove the clicked accordion
      accordions.splice($('.accordion').index($(this)),1);
      
      // Deactivate the other accordions
      $(accordions).accordion('option','active',false);
    });
});

,

以下代码片段可帮助您实现您想要的

$(function() {
  $(".accordion").accordion({
    heightStyle: "content",active: false,beforeActivate: function( event,ui ) { 
        $(".ui-accordion-content").slideUp(1000);
    }
  });
});