简体   繁体   English

如何使用Javascript / jquery切换手风琴

[英]How to toggle accordion using Javascript/jquery

I'm trying to get my accordion to toggle but nothing seems to be happening. 我试图让我的手风琴切换,但似乎什么也没发生。 By toggle I mean clicking the same accordion header should close it as well. 通过切换,我的意思是单击相同的手风琴标题也应将其关闭。 So far I've managed to get it to work by clicking another header the previous one will close, but closing an open one and keeping them all closed seems to not be working. 到目前为止,我已经设法通过单击另一个标题(前一个标题将关闭)使其工作,但是关闭一个打开的标题并将其全部关闭似乎无效。

here is my script: 这是我的脚本:

$(document).ready(function() {
  $("#accordion section h1").click(function(e) {
    $(this).parents().siblings("section").addClass("ac_hidden");
    $(this).parents("section").removeClass("ac_hidden");
    $(this).collapse('toggle')
    e.preventDefault();
  });

});

I've tried playing the collapse outside of the function $(#accordion).collapse('toggle'), I've tried to use ('hide')` as well but no luck. 我试过在函数$(#accordion).collapse('toggle'), I've tried to use之外进行折叠$(#accordion).collapse('toggle'), I've tried to use ('hide')`,但是没有运气。

here is a jsfiddle of it: Jsfiddle 这是它的一个jsfiddle: Jsfiddle

Any suggestions would be greatly appreciated! 任何建议将不胜感激!

Thanks 谢谢

Try this 尝试这个

$("#accordion section h1").click(function(e) {
  if (!$(this).closest('section').hasClass('ac_hidden')) {
    $(this).closest("section").addClass("ac_hidden");
  } else {
    $(this).closest("section").siblings().addClass("ac_hidden");
    $(this).closest("section").removeClass("ac_hidden");
  }
  e.preventDefault();
});

I've changed .parents() to .closest() as it's a better selector for this scenario, it will grab the closest section to the clicked header which is what we want, I've then just added a check to make sure the current element doesn't have the ac_hidden class and if not then add it (slide the current one up) else we are sliding a different one down and the functionality is what you pretty much had already. 我已经将.parents()更改为.closest()因为它是此方案的更好选择器,它将抓住我们想要的最接近被单击标题的部分,然后我添加了一个检查以确保当前元素没有ac_hidden类,如果没有,则将其添加(将当前元素向上滑动),否则我们将另一元素向下滑动,而功能几乎就是您已经拥有的。 Hope that helps. 希望能有所帮助。

jsFiddle jsFiddle

This is the javascript code for the accordion on my website, benstechgarage.com/accordion.html 这是我的网站benstechgarage.com/accordion.html上的手风琴的javascript代码

$('.accordion').on('click', '.accordion-control', function(e) {
      e.preventDefault();
      $(this)
        .next('.accordion-panel')
        .not(':animated')
        .slideToggle();
    });

you just have to check if it have the class so you can close it if( !$(this).parents("section").hasClass("ac_hidden")){ $(this).parents().addClass("ac_hidden"); console.log(1) return; } 您只需要检查它是否具有该类,即可关闭if( !$(this).parents("section").hasClass("ac_hidden")){ $(this).parents().addClass("ac_hidden"); console.log(1) return; } if( !$(this).parents("section").hasClass("ac_hidden")){ $(this).parents().addClass("ac_hidden"); console.log(1) return; }

check this out: https://jsfiddle.net/jxyozw5c/3/ 检查一下: https : //jsfiddle.net/jxyozw5c/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM