簡體   English   中英

簡單的jQuery手風琴切換

[英]Simple jQuery accordion toggle

我正在使用jQuery切換狀態的簡單手風琴。 到目前為止,一切都很好,但是當我單擊的開關與最初單擊時所單擊的開關不同時,它將自動進行開關,而前一個開關處於打開狀態。

如何進行某種檢查以查看是否打開了另一個檢查並同時切換其狀態?

這是小提琴。 隨便擺弄幾秒鍾,您就會明白我的意思。

// Accordion init
// Hide accordion content on load
$('.accordion-content').hide();
$('.widget-accordion').find('.accordion-toggle').click(function() {

  // State change visuals
  $(this).toggleClass('opened');

  //Expand or collapse this panel
 $(this).next().slideToggle('fast');

  //Hide the other panels
  $(".accordion-content").not($(this).next()).slideUp('fast');

});

正如我在上面的評論中所述,這里是一個工作的小提琴。

問題是,當您打開一個手風琴項目時,不允許打開其他項目,這意味着您必須清除代表打開狀態的每個類。

工作小提琴

// Accordion init
// Hide accordion content on load
$('.accordion-content').hide();
$('.widget-accordion').find('.accordion-toggle').click(function() {
    $('.accordion-toggle').not(this).each(function(){
    $(this).removeClass("opened");
  });
  // State change visuals
  $(this).toggleClass('opened');

  //Expand or collapse this panel
  $(this).next().slideToggle('fast');

  //Hide the other panels
  $(".accordion-content").not($(this).next()).slideUp('fast');

});

使用This而不是toggleClass

// State change visuals
 $('.accordion-toggle').removeClass('opened');
 $(this).addClass('opened');

小提琴: https : //jsfiddle.net/cndnnww5/5/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM