如果单击相同的按钮,则关闭手风琴

时间:2019-06-15 07:46:10

标签: javascript jquery html css

我的网站上有手风琴。运行良好。如果一个手风琴打开并单击第二个,则它将关闭前一个。到现在为止没有问题。

我的问题是,如果单击手风琴,必须关闭它。我的意思是,如果手风琴是打开的,然后单击它,那它应该是关闭的。

可以检查我的脚本,在哪里添加关闭脚本? 我尝试过

 $('.ac-pane.active .ac-title').click(function(){
 getparent.find('.ac-pane').removeClass('active');
 //  $(this).next('.ac-content').slideUp();
 $('.ac-pane .ac-content').css('display', 'none');

 });

//Accordion
$(function() {
  'use strict';

  var selector = $('.ac-title');

  $('.accordion-wrapper').each(function() {
    if ($(this).find('.ac-pane').hasClass('active')) {
      $('.ac-pane.active .ac-content').css('display', 'block');
    }
  });

  selector.on('click', function(event) {
    event.preventDefault();

    // get the attr value
    var attr = selector.attr('data-accordion');
    var getparent = $(this).closest('.accordion-wrapper');

    if ($(this).attr('data-accordion') == 'true') {

      if ($(this).next('.ac-content').is(':visible')) {
        return false;
      } else {

        getparent.find('.ac-content').slideUp();
        $(this).next('.ac-content').slideDown();
        getparent.find('.ac-pane').removeClass('active');
        $(this).parent().addClass('active');
      }

    } else {
      $(this).next('.ac-content').slideToggle();
      $(this).parent().toggleClass('active');
    }

  });
});
.ac-pane {
  margin-bottom: 15px;
  color: #000;
}

.ac-pane:last-child {
  margin-bottom: 0;
}

.ac-content {
  display: none;
}

.ac-title {
  /*border: 1px solid #000;*/
  color: #000;
  display: block;
  padding: 12px;
}

.ac-title i {
  float: right;
  font-size: 20px;
}

.ac-title i:before {
  content: "\f107";
}

.active .ac-title i:before {
  content: "\f106";
}

.ac-content {
  border: 1px solid #fff;
  margin-top: -1px;
  padding: 15px;
}
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">
<section>
  <div class="container">
    <div class="row">
      <div class="col-sm-12">
        <div class="heading">
          <h2>Accordion Demo</h2>
        </div>
        <div class="accordion-wrapper">
          <div class="ac-pane active">
            <a href="#" class="ac-title" data-accordion="true">
              <span>Hello World</span>
              <i class="fa"></i>
            </a>
            <div class="ac-content">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil ullam architecto earum reprehenderit dolore iusto labore sed, sunt, quasi soluta assumenda, a quam aspernatur repudiandae enim. Architecto, assumenda tempore vitae!
            </div>
          </div>
          <div class="ac-pane">
            <a href="#" class="ac-title" data-accordion="true">
              <span>Hello World</span>
              <i class="fa"></i>
            </a>
            <div class="ac-content">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil ullam architecto earum reprehenderit dolore iusto labore sed, sunt, quasi soluta assumenda, a quam aspernatur repudiandae enim. Architecto, assumenda tempore vitae!
            </div>
          </div>
          <div class="ac-pane">
            <a href="#" class="ac-title" data-accordion="true">
              <span>Hello World</span>
              <i class="fa"></i>
            </a>
            <div class="ac-content">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil ullam architecto earum reprehenderit dolore iusto labore sed, sunt, quasi soluta assumenda, a quam aspernatur repudiandae enim. Architecto, assumenda tempore vitae!
            </div>
          </div>
          <div class="ac-pane">
            <a href="#" class="ac-title" data-accordion="true">
              <span>Hello World</span>
              <i class="fa"></i>
            </a>
            <div class="ac-content">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil ullam architecto earum reprehenderit dolore iusto labore sed, sunt, quasi soluta assumenda, a quam aspernatur repudiandae enim. Architecto, assumenda tempore vitae!
            </div>
          </div>
        </div>
      </div>


    </div>
  </div>
</section>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

2 个答案:

答案 0 :(得分:1)

如果可见手风琴

$(this).next('.ac-content').slideUp();
$(this).parent().toggleClass('active');

演示

//Accordion
$(function() {
  'use strict';

  var selector = $('.ac-title');

  $('.accordion-wrapper').each(function() {
    if ($(this).find('.ac-pane').hasClass('active')) {
      $('.ac-pane.active .ac-content').css('display', 'block');
    }
  });

  selector.on('click', function(event) {
    event.preventDefault();

    // get the attr value
    var attr = selector.attr('data-accordion');
    var getparent = $(this).closest('.accordion-wrapper');

    if ($(this).attr('data-accordion') == 'true') {

      if ($(this).next('.ac-content').is(':visible')) {
        $(this).next('.ac-content').slideUp();
        $(this).parent().toggleClass('active');
      } else {

        getparent.find('.ac-content').slideUp();
        $(this).next('.ac-content').slideDown();
        getparent.find('.ac-pane').removeClass('active');
        $(this).parent().addClass('active');
      }

    } else {
      $(this).next('.ac-content').slideToggle();
      $(this).parent().toggleClass('active');
    }

  });
});
.ac-pane {
  margin-bottom: 15px;
  color: #000;
}

.ac-pane:last-child {
  margin-bottom: 0;
}

.ac-content {
  display: none;
}

.ac-title {
  /*border: 1px solid #000;*/
  color: #000;
  display: block;
  padding: 12px;
}

.ac-title i {
  float: right;
  font-size: 20px;
}

.ac-title i:before {
  content: "\f107";
}

.active .ac-title i:before {
  content: "\f106";
}

.ac-content {
  border: 1px solid #fff;
  margin-top: -1px;
  padding: 15px;
}
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">

<section>
  <div class="container">
    <div class="row">
      <div class="col-sm-12">
        <div class="heading">
          <h2>Accordion Demo</h2>
        </div>
        <div class="accordion-wrapper">
          <div class="ac-pane active">
            <a href="#" class="ac-title" data-accordion="true">
              <span>Hello World</span>
              <i class="fa"></i>
            </a>
            <div class="ac-content">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil ullam architecto earum reprehenderit dolore iusto labore sed, sunt, quasi soluta assumenda, a quam aspernatur repudiandae enim. Architecto, assumenda tempore vitae!
            </div>
          </div>
          <div class="ac-pane">
            <a href="#" class="ac-title" data-accordion="true">
              <span>Hello World</span>
              <i class="fa"></i>
            </a>
            <div class="ac-content">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil ullam architecto earum reprehenderit dolore iusto labore sed, sunt, quasi soluta assumenda, a quam aspernatur repudiandae enim. Architecto, assumenda tempore vitae!
            </div>
          </div>
          <div class="ac-pane">
            <a href="#" class="ac-title" data-accordion="true">
              <span>Hello World</span>
              <i class="fa"></i>
            </a>
            <div class="ac-content">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil ullam architecto earum reprehenderit dolore iusto labore sed, sunt, quasi soluta assumenda, a quam aspernatur repudiandae enim. Architecto, assumenda tempore vitae!
            </div>
          </div>
          <div class="ac-pane">
            <a href="#" class="ac-title" data-accordion="true">
              <span>Hello World</span>
              <i class="fa"></i>
            </a>
            <div class="ac-content">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil ullam architecto earum reprehenderit dolore iusto labore sed, sunt, quasi soluta assumenda, a quam aspernatur repudiandae enim. Architecto, assumenda tempore vitae!
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

答案 1 :(得分:1)

您可以执行类似的操作,选择被单击元素的parent,并从同级元素和slideToggle被单击元素的内容中删除活动类。

$('.ac-pane.active .ac-content').css('display', 'block');

$(".ac-title").click(function(e) {
  e.preventDefault();
  
  const parent = $(this).parent('.ac-pane');
  const siblings = parent.siblings('.ac-pane');
  
  siblings.removeClass('active');
  siblings.find('.ac-content').slideUp();
  
  parent.toggleClass('active');
  parent.find('.ac-content').slideToggle();
})
.ac-pane {
  margin-bottom: 15px;
  color: #000;
}

.ac-pane:last-child {
  margin-bottom: 0;
}

.ac-content {
  display: none;
}

.ac-title {
  /*border: 1px solid #000;*/
  color: #000;
  display: block;
  padding: 12px;
}

.ac-title i {
  float: right;
  font-size: 20px;
}

.ac-title i:before {
  content: "\f107";
}

.active .ac-title i:before {
  content: "\f106";
}

.ac-content {
  border: 1px solid #fff;
  margin-top: -1px;
  padding: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">
<section>
  <div class="container">
    <div class="row">
      <div class="col-sm-12">
        <div class="heading">
          <h2>Accordion Demo</h2>
        </div>
        <div class="accordion-wrapper">
          <div class="ac-pane active">
            <a href="#" class="ac-title" data-accordion="true">
              <span>Hello World</span>
              <i class="fa"></i>
            </a>
            <div class="ac-content">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil ullam architecto earum reprehenderit dolore iusto labore sed, sunt, quasi soluta assumenda, a quam aspernatur repudiandae enim. Architecto, assumenda tempore vitae!
            </div>
          </div>
          <div class="ac-pane">
            <a href="#" class="ac-title" data-accordion="true">
              <span>Hello World</span>
              <i class="fa"></i>
            </a>
            <div class="ac-content">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil ullam architecto earum reprehenderit dolore iusto labore sed, sunt, quasi soluta assumenda, a quam aspernatur repudiandae enim. Architecto, assumenda tempore vitae!
            </div>
          </div>
          <div class="ac-pane">
            <a href="#" class="ac-title" data-accordion="true">
              <span>Hello World</span>
              <i class="fa"></i>
            </a>
            <div class="ac-content">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil ullam architecto earum reprehenderit dolore iusto labore sed, sunt, quasi soluta assumenda, a quam aspernatur repudiandae enim. Architecto, assumenda tempore vitae!
            </div>
          </div>
          <div class="ac-pane">
            <a href="#" class="ac-title" data-accordion="true">
              <span>Hello World</span>
              <i class="fa"></i>
            </a>
            <div class="ac-content">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil ullam architecto earum reprehenderit dolore iusto labore sed, sunt, quasi soluta assumenda, a quam aspernatur repudiandae enim. Architecto, assumenda tempore vitae!
            </div>
          </div>
        </div>
      </div>


    </div>
  </div>
</section>