简体   繁体   中英

jquery add active class to a tag

I am using Jquery reveal to show hide div with anchor tag i am trying to add class ('active') to a tag when i click on it to show hidden div then there should be 'active' class appear like Open/Close i have used .addClass('active') it work but when i again click on anchor the class="active" doesn't disappear.

here is my code :

<script type="text/javascript">  
    $(document).ready(function(){

        $(".slide_div").hide();
        $(".show_hide").show().addClass('active');

        $('.show_hide').click(function(){        
            $(".slide_div").slideToggle();
        });

    });
</script>

<a class="show_hide" href="#">Open/Close</a>
<div class="slide_div"></div>

Use $(".show_hide").toggleClass('active');

$('.show_hide').click(function(){
    $(this).toggleClass('active');
    $(".slide_div").slideToggle();
});

Demo: Fiddle

Use toggleClass("active") instead of addClass('active')

http://api.jquery.com/toggleClass/

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