现在此功能隐藏所有显示的答案。我需要它只隐藏属于第二次单击的特定问题的答案。我对 JQuery 很陌生,因此可能有一个简单的解决方案,但任何帮助都会很棒。
$(document).ready(function() {
$(".question").click(function () {
$('.answer').hide(); // hides open answers before showing a new answer
if ($(this).next().is(":hidden")) { // check the visibility of the next element in the DOM
$(this).next().show(); // show it
} else {
$(this).next().hide(); // hide it
}
});
});
最佳答案
由于没有 HTML 可供引用,我模拟了一个示例 here :
HTML
<span class="question">What is your name?</span>
<span class="answer">Chase</span>
<br/>
<label class="question">How old are you?</label>
<span class="answer">25</span>
<br/>
<label class="question">What is your favorite color?</label>
<span class="answer">Blue</span>
<br/>
<label class="question">Do you like cheese?</label>
<span class="answer">Duh</span>
JavaScript/jQuery
$(document).ready(function() {
$(".question").click(function () {
$(this).next("span.answer").fadeToggle();
});
});
我正在使用next
方法来获取点击
的问题
的以下.answer
。
关于javascript - 任务时只需要隐藏特定的 '.answer'。再次点击该答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15979651/