Add class then remove class when click

前端 未结 2 1392
故里飘歌
故里飘歌 2021-01-28 10:03

I have an anchor text that when click adds an active class to another class. I want to remove it when it has an active class or if I click the anchor text again. How can I do th

相关标签:
2条回答
  • 2021-01-28 10:54

    try this example

        <style type="text/css">
            .sector-active
            {
                color: green;
            }
        </style>
        <div class="container">
           <div class="main-sectors">
               <a href="#" class="sector-active">Name1</a>
               <a href="#" class="p-l-35">Name2</a>
               <a href="#" class="p-l-35">Name3</a>
               <a href="#" class="p-l-35">Name4</a>
               <a href="#" class="p-l-35">Name5</a>
           </div>
        </div>
       <script type="text/javascript" src="../library/jquery-3.2.1.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function()
            {
                $('a').click(function()
                {
                    $(this).removeClass('p-1-35');
                    $('a').removeClass('sector-active');
                    $(this).addClass('sector-active');
                });
            });
    
        </script>
    

    this code will be add sector-active class to clicked a tag and remove sector-active class from another a tag

    if you can add sector-active class to all clicked a tag then comment to following line

    // $('a').removeClass('sector-active');
    

    this will help you

    0 讨论(0)
  • 2021-01-28 10:58

    Try this

    $(function(){
      $("#menu1").click(function(){
          $(this).toggleClass("active");
          $("a.mm-title").toggleClass("remove");
      });
     });
    

    Let me know if it is helpful

    0 讨论(0)
提交回复
热议问题