以下のようにアコーディオンパネル?を作りたいと思っていて、+マークをクリックすると、答えが出て、-マークをクリックすると、閉じるようにしたいのですが、うまく行きませんでした。どこがおかしいのか見ていただけると幸いです。
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>Practice</title> <link rel="stylesheet" href="css/styles.css"> </head> <body> <h2>Q&A</h2> <div class="faq"> <ul id="faq-list"> <li class="faq-list-items"> <h3>○○○○○○○○○○○○○○○○○○○○○○○○?</h3> <span>+</span> <div class="answer"> <p>○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○です。</p> </div> </li> <li class="faq-list-items"> <h3>○○○○○○○○○○○○○○○○○○○○○○○○?</h3> <span>+</span> <div class="answer"> <p>○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○です。</p> </div> </li> <li class="faq-list-items"> <h3>○○○○○○○○○○○○○○○○○○○○○○○○?</h3> <span>+</span> <div class="answer"> <p>○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○です。</p> </div> </li> </ul> </div> <div id="top-btn">TOPに戻る</div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="js/main.js"></script> </body> </html>
$(function() { $('.option-btn').click(function() { $(this).css('color', 'red'); }); $('#top-btn').click(function() { $('html, body').scrollTop(0); }); $('.fad-list-item').click(function() { var $answer = $(this).find('.answer'); if($answer.hasClass('open')){ $answer.removeClass('open'); $answer.slideUp(); $(this).find('span').text('+'); } else { $answer.addClass('open'); $answer.slideDown(); $(this).find('span').text('-'); } }); });
body { font-family: Arial, sans-serif; font-size: 18px; } #top-btn { cursor: pointer; background: #3498db; color: #fff; width: 250px; height: 50px; text-align: center; line-height: 50px; border-radius: 10px; margin: 0 auto; margin-top: 30px; } .faq-list-items{ list-style: none; position: relative; } .answer { display: none; font-size: 12px; padding: 5px 0px; margin-bottom: 30px; } .faq-list-items > h3 { font-size: 14px; } .faq-list-items span { position: absolute; cursor: pointer; top: 0; right: 5px; color: #ccc; font-size: 13px; }

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/16 04:25