开发者

Simple accordion menu (jQuery)

开发者 https://www.devze.com 2022-12-23 13:58 出处:网络
jQuery: // ACCORDION $(\'.acco开发者_StackOverflowrdion .answer\').hide(); // hide all $(\'.accordion .question\').click(function(){

jQuery:

// ACCORDION
$('.acco开发者_StackOverflowrdion .answer').hide(); // hide all
$('.accordion .question').click(function(){
     $('.accordion .answer').slideUp(); // hide all open
     $(this).addClass('active').next().slideDown(); // show the anwser
     return false;
});

HTML:

<dl class="accordion">
    <dt class="question">question</dt>
    <dd class="answer">answer</dd>
    <dt class="question">question</dt>
    <dd class="answer">answer</dd>
</dl>

... works, but

  • The 'active' class must be removed from inactive question elements &
  • At least one of the answers remains open; all answers should be able to close.

Thanks!


Okay, figured it:

$('.accordion .answer').hide(); // hide all
$(".accordion .question").click(function () {
    $(this).toggleClass('active').next(".answer").slideToggle(300).siblings(".answer").slideUp("slow");
    $(this).siblings().removeClass('active');
});


$('.accordion .answer').hide(); // hide all
$('.accordion .question').click(function(){
    if($(this).hasClass('active')) {
        $(this).removeClass('active').next().slideUp();
    } else {
        $('.accordion .answer').removeClass('active').slideUp();
        $(this).addClass('active').next().slideDown();
    }
    return false;
});

But you should really take a look at the jQuery UI Accordion.

0

精彩评论

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