jQuery 手风琴不会删除单击上一个手风琴的类

Posted

技术标签:

【中文标题】jQuery 手风琴不会删除单击上一个手风琴的类【英文标题】:jQuery accordion doesn't remove the class on click on the previous accordion 【发布时间】:2021-04-19 14:11:53 【问题描述】:

我正在制作一个常见问题解答部分,但在使手风琴正确运行时遇到了一些麻烦。

当我一个一个点击它们并在打开下一个之前关闭它们,一切都很好。但是,如果我打开一个,让它保持打开状态,然后单击下一个,“显示”类仍保留在前一个上。

我希望我说清楚了,你可以在我的代码中看到我在说什么:

$(".js-accordion").click(function(e) 
  e.preventDefault();

  var $this = $(this);

  if ($this.next().hasClass("show")) 
    $this.removeClass("show");
    $this.next().removeClass("show");
    $this.next().stop().slideUp(350);
   else 
    $this.addClass("show");
    $this.parent().parent().find("div .accordion__inner").removeClass("show");
    $this.parent().parent().find("div .accordion__inner").stop().slideUp(350);
    $this.next().stop().toggleClass("show");
    $this.next().stop().slideToggle(350);
  
);
.accordion__box 
  max-width: 600px;
  margin: 20px auto;
  box-shadow: 0px 14px 20px 5px rgba(0, 0, 0, 0.1);


.accordion__inner 
  overflow: hidden;
  display: none;
  padding: 10px 20px;
  position: relative;
  background-color: #fff;
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;


.accordion__header 
  border-radius: 5px;
  font-weight: 900;
  font-size: 24px;
  font-family: "Lato", sans-serif;
  margin-bottom: 0;
  color: #fff;
  display: flex;
  justify-content: space-between;
  background-color: #004a80;
  padding: 20px 30px 20px 40px;
  cursor: pointer;
  transition: all 0.3s ease-in-out;


.accordion__header.show 
  background-color: #EF2A72;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;


.accordion__header.show::after 
  transition: all 0.3s ease-in-out;
  transform: rotate(180deg);


.accordion__header::after 
  content: "\f107";
  color: #fff;
  max-width: 30px;
  height: 30px;
  width: 100%;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: "Font Awesome 5 Pro";
  font-size: 30px;
  font-weight: 500;
  transition: all 0.3s ease-in-out;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="accordion">
  <div class="accordion__box">
    <div class="accordion__header js-accordion">Who is this program for?</div>
    <div class="accordion__inner">
      <p>I’ve created this program for busy women of any age. No matter what your current weight… fitness level… or schedule looks like — this workout is for you. If you want to get a lifted, toned and curvy booty that’s also strong and athletic, this program
        is yours.</p>
    </div>
  </div>
  <div class="accordion__box">
    <div class="accordion__header js-accordion">Why is this method better than others?</div>
    <div class="accordion__inner">
      <p>I’ve created this program for busy women of any age. No matter what your current weight… fitness level… or schedule looks like — this workout is for you. If you want to get a lifted, toned and curvy booty that’s also strong and athletic, this program
        is yours.</p>
    </div>
  </div>
</div>

https://codepen.io/christmastrex/pen/oNzQEva

【问题讨论】:

【参考方案1】:

您可以使用.not() 排除所有不在accordion__box 内的元素,其中js-accordion 被点击。

演示代码

$(".js-accordion").click(function(e) 
  e.preventDefault();

  var $this = $(this);
  $this.toggleClass("show")//add show to one which is clicked
  $this.closest(".accordion").find(".js-accordion").not($this).removeClass("show")//remove from other
  $this.next().slideToggle(350);//show accrodian
  $this.closest(".accordion").find(".accordion__inner").not($this.next()).stop().slideUp(350);//slideup another


);
.accordion__box 
  max-width: 600px;
  margin: 20px auto;
  box-shadow: 0px 14px 20px 5px rgba(0, 0, 0, 0.1);


.accordion__inner 
  overflow: hidden;
  display: none;
  padding: 10px 20px;
  position: relative;
  background-color: #fff;
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;


.accordion__header 
  border-radius: 5px;
  font-weight: 900;
  font-size: 24px;
  font-family: "Lato", sans-serif;
  margin-bottom: 0;
  color: #fff;
  display: flex;
  justify-content: space-between;
  background-color: #004a80;
  padding: 20px 30px 20px 40px;
  cursor: pointer;
  transition: all 0.3s ease-in-out;


.accordion__header.show 
  background-color: #EF2A72;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;


.accordion__header.show::after 
  transition: all 0.3s ease-in-out;
  transform: rotate(180deg);


.accordion__header::after 
  content: "\f107";
  color: #fff;
  max-width: 30px;
  height: 30px;
  width: 100%;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: "Font Awesome 5 Pro";
  font-size: 30px;
  font-weight: 500;
  transition: all 0.3s ease-in-out;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="accordion">
  <div class="accordion__box">
    <div class="accordion__header js-accordion">Who is this program for?</div>
    <div class="accordion__inner">
      <p>I’ve created this program for busy women of any age. No matter what your current weight… fitness level… or schedule looks like — this workout is for you. If you want to get a lifted, toned and curvy booty that’s also strong and athletic, this program
        is yours.</p>
    </div>
  </div>
  <div class="accordion__box">
    <div class="accordion__header js-accordion">Why is this method better than others?</div>
    <div class="accordion__inner">
      <p>I’ve created this program for busy women of any age. No matter what your current weight… fitness level… or schedule looks like — this workout is for you. If you want to get a lifted, toned and curvy booty that’s also strong and athletic, this program
        is yours.</p>
    </div>
  </div>
</div>

【讨论】:

非常感谢!正是我在寻找

以上是关于jQuery 手风琴不会删除单击上一个手风琴的类的主要内容,如果未能解决你的问题,请参考以下文章

让 MVC 4 验证 jquery 手风琴表单的所有部分

仅向单击的手风琴面板添加或删除类

如何使用 ajax 加载将 jquery 手风琴加载到 div 中?

删除并创建带有内容窗格的 dojo 手风琴容器

如何一次打开所有 Bootstrap 手风琴标签

JQuery UI:手风琴回调