简体   繁体   中英

updating my js accordion tabs to selectively open and close content

I have created an accordion in such a way that only 1 set of content can be open at any one time, my problem is how do I update my code so I can click the active tab and close the content? At the moment if I try to close the active tab the content slides up and then slides straight back down?

Im sure I've written this code in the wrong way and would appreciate all advice on how to enhance this http://jsfiddle.net/kyllle/csggQ/1/

Kyle

Seems you are over complicating things :)

You've kinda already made it, http://jsfiddle.net/csggQ/21/

$(document).ready(function(){
    $('p').hide();

    $('h2').click(function() {

        if($(this).hasClass('active'))
        {
           $(this).removeClass('active');
           $(this).next('p').slideUp(600);
        } else {          
           $('#myContent .active').removeClass('active').next().slideUp(600);
           $(this).addClass('active');
           $(this).next('p').slideDown(600);
        }

    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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