开发者

Is there an efficient way of doing jQuery toggle?

开发者 https://www.devze.com 2023-04-01 18:26 出处:网络
I was creating a FAQ section for my site, and, was wondering if there\'s more efficient way of toggling answers when the question is clicked. So far, I have the below code. However, it opens all answe

I was creating a FAQ section for my site, and, was wondering if there's more efficient way of toggling answers when the question is clicked. So far, I have the below code. However, it opens all answers when any question is clicked. If I wanna do it one by one, I have 100 question, and have to write 100 lines of toggle for answer-1, answer-2...So, is there a solution for this?

$(开发者_运维知识库function(){
    $("a.question").click(function(){
    $("p.answer").toggle("slow");
 });
});

HTML + CSS:

a.question {display:block}
p {display:none}

<a class="question">Question 1?</a>
<p class="answer">Answer 1</p>

<a class="question">Question 2?</a>
<p class="answer">Answer 2</p>

<a class="question">Question 3?</a>
<p class="answer">Answer 3</p>


$(function(){
    $("a.question").click(function(){
    $(this).next("p.answer").toggle("slow");
 });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消