LoginSignup
1
3

More than 1 year has passed since last update.

【jQuery】FAQアコーディオンの作成方法

Posted at

完成形

  • SCSSを使用
  • jQueryを使用

See the Pen abJKBrL by c-koch0514 (@c-koch0514) on CodePen.


HTML

html
<section id="faq">
  <dl id="faq-item">
    <dt class="question">
      <p class="question-text">質問1</p>
      <p class="plus">+</p>
    </dt>
    <dd class="answer">回答1</dd>
    <dt class="question">
      <p class="question-text">質問2</p>
      <p class="plus">+</p>
    </dt>
    <dd class="answer">回答2</dd>
    <dt class="question">
      <p class="question-text">質問3</p>
      <p class="plus">+</p>
    </dt>
    <dd class="answer">回答3</dd>
  </dl>
</section>

SCSS

scss
/***********************************************************************/
/**********                FAQの全体                       *************/
/***********************************************************************/
#faq {
  #faq-item {
    width: 250px;
    margin: 0 auto;
    background-color: #eee;
    font-size: 15px;
    cursor: pointer;

    .question {
      display: flex;
      justify-content: space-between;
    }

    .answer {
      display: none;
    }
  }
}
/***********************  ena of faq    ********************************/

jQuery

jQuery
$(function(){  

  /***************** FQAアコーディオン **********************/
  $('.question').on('click', function () {
    $(this).next().slideToggle();

    if( $(this).hasClass('close')){ 
      $(this).find('.plus').text("+");
    }
    else{
      $(this).find('.plus').text("");
    }
    $(this).toggleClass('close');
  });
  /*********************************************************/

});
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3