简体   繁体   English

在常见问题解答中单击的元素的 toggleClass 图标

[英]toggleClass icon of element clicked on in a faq

I am trying to switch the icon of the question once I click on it, I almost got it to work, the icon switches when you click on a question and switches back when you click on the same question again.一旦我点击它,我试图切换问题的图标,我几乎让它工作,当你点击一个问题时图标会切换,当你再次点击同一个问题时会切换回来。

It goes wrong when you click on a question and then on another question.当您单击一个问题然后单击另一个问题时,它会出错。 The first question you clicked on is automatically closing but the icon doesn't switch back.您单击的第一个问题是自动关闭,但图标不会切换回来。

 $('.answer').hide(); $('.question').click(function(){ var $this = $(this).parent().find('.answer'); $(".answer").not($this).hide(); $this.toggle(); $(this).toggleClass('faqmin') });
 * { border:0; padding:0; margin:0; }.faq { width: 100%; padding:10px 0 10px 0; border:0; border-bottom: 1px solid #e0e0e0; outline: none; cursor: pointer; }.faqplus:after { float: right; margin-left: 5px; content: '\002B'; font-size:18px; color: black; }.faqmin:after { content: "\2212"; }.answer { margin-top:10px; overflow: hidden; transition: max-height 0.2s ease-out; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="faq"> <div class="question faqplus">Question</div> <div class="answer">Answer</div> </div> <div class="faq"> <div class="question faqplus">Question</div> <div class="answer">Answer</div> </div> <div class="faq"> <div class="question faqplus">Question</div> <div class="answer">Answer</div> </div>

When user click, you need to reset class of all questions.当用户点击时,您需要重置所有问题的 class。

https://api.jquery.com/attr/ https://api.jquery.com/attr/

 $('.answer').hide(); $('.question').click(function(){ $('.question').attr('class', 'question faqplus'); // here var $this = $(this).parent().find('.answer'); $(".answer").not($this).hide(); $this.toggle(); $(this).toggleClass('faqmin') });
 * { border:0; padding:0; margin:0; }.faq { width: 100%; padding:10px 0 10px 0; border:0; border-bottom: 1px solid #e0e0e0; outline: none; cursor: pointer; }.faqplus:after { float: right; margin-left: 5px; content: '\002B'; font-size:18px; color: black; }.faqmin:after { content: "\2212"; }.answer { margin-top:10px; overflow: hidden; transition: max-height 0.2s ease-out; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="faq"> <div class="question faqplus">Question</div> <div class="answer">Answer</div> </div> <div class="faq"> <div class="question faqplus">Question</div> <div class="answer">Answer</div> </div> <div class="faq"> <div class="question faqplus">Question</div> <div class="answer">Answer</div> </div>

You need to toggle the plus/minus in the other question yourself:您需要自己在另一个问题中切换加号/减号:

$(".faqmin").not(this).toggleClass('faqmin')

Note using $this to mean something other than $(this) can be highly confusing, so I've changed that variable in the snippet below.请注意,使用$this来表示$(this)以外的其他内容可能会非常令人困惑,因此我在下面的代码段中更改了该变量。

 $('.answer').hide(); $('.question').click(function(){ var thisans = $(this).parent().find('.answer'); $(".answer").not(thisans).hide(); $(".faqmin").not(this).toggleClass('faqmin') thisans.toggle(); $(this).toggleClass('faqmin') });
 * { border:0; padding:0; margin:0; }.faq { width: 100%; padding:10px 0 10px 0; border:0; border-bottom: 1px solid #e0e0e0; outline: none; cursor: pointer; }.faqplus:after { float: right; margin-left: 5px; content: '\002B'; font-size:18px; color: black; }.faqmin:after { content: "\2212"; }.answer { margin-top:10px; overflow: hidden; transition: max-height 0.2s ease-out; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="faq"> <div class="question faqplus">Question</div> <div class="answer">Answer</div> </div> <div class="faq"> <div class="question faqplus">Question</div> <div class="answer">Answer</div> </div> <div class="faq"> <div class="question faqplus">Question</div> <div class="answer">Answer</div> </div>


Note: given your html setup, you can toggle the answer with css注意:鉴于您的 html 设置,您可以使用 css 切换答案

 $('.question').click(function(){ $(".faqmin").not(this).removeClass('faqmin'); $(this).toggleClass('faqmin'); });
 * { border:0; padding:0; margin:0; }.faq { width: 100%; padding:10px 0 10px 0; border:0; border-bottom: 1px solid #e0e0e0; outline: none; cursor: pointer; }.faqplus:after { float: right; margin-left: 5px; content: '\002B'; font-size:18px; color: black; }.faqmin:after { content: "\2212"; }.answer { margin-top:10px; overflow: hidden; transition: max-height 0.2s ease-out; display:none; }.faqmin +.answer { display:block; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="faq"> <div class="question faqplus">Question</div> <div class="answer">Answer</div> </div> <div class="faq"> <div class="question faqplus">Question</div> <div class="answer">Answer</div> </div> <div class="faq"> <div class="question faqplus">Question</div> <div class="answer">Answer</div> </div>

and if you add a styled checkbox, you don't need any js at all, leave that one to you.如果你添加一个样式复选框,你根本不需要任何 js,把那个留给你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM