Open one tab at a time in accordion

为君一笑 提交于 2019-12-02 08:14:50

问题


I have a accordion which is working absolutely fine but what I need is to open only one tab at a time, means when one tab is opened then another tab should be closed.

Currently you can see that we can open all the tabs by clicking on tab links.

Code here

$("#accordion > li > span").click(function() {
  $(this).siblings("div").slideToggle(250);
        $(this).toggleClass("active");

});

Here is the Fiddle


回答1:


LIVE DEMO

$("#accordion > li > span").click(function() {
    $(this).closest('li').siblings().find('span').removeClass('active').next('div').slideUp(250);
    $(this).toggleClass("active").next('div').slideToggle(250);
});


Or like: LIVE DEMO
$("#accordion > li > span").click(function() {
    $(this).toggleClass("active").next('div').slideToggle(250)
    .closest('li').siblings().find('span').removeClass('active').next('div').slideUp(250);
});



回答2:


You can also close other accordions by adding a beforeActivate callback:

$( ".myAccordion" ).accordion({
      collapsible: true,
      active: false,
    heightStyle: "content",
    beforeActivate: function(event, ui) {
   $( ".myAccordion" ).not(this).accordion('option', 'active', false);
}


来源:https://stackoverflow.com/questions/15739045/open-one-tab-at-a-time-in-accordion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!