jquery add remove class onclick

前端 未结 3 1366
抹茶落季
抹茶落季 2021-02-01 09:46

i have any navigation menu !

   
  • HOME
相关标签:
3条回答
  • 2021-02-01 10:28
    $("a").click(function()
    {
      $("a").parent().removeClass("selected");
    
      $(this).parent().addClass("selected");
    });
    

    Live Demo

    0 讨论(0)
  • 2021-02-01 10:33

    On the assumption that you want to have only one element of class-name selected I suggest:

    $('ul.navigation li a').click(
        function(e) {
            e.preventDefault(); // prevent the default action
            e.stopPropagation(); // stop the click from bubbling
            $(this).closest('ul').find('.selected').removeClass('selected');
            $(this).parent().addClass('selected');
        });
    

    JS Fiddle demo.

    0 讨论(0)
  • 2021-02-01 10:34

    you can use toggleClass:

    $("#myElement1").click(function(){
       $("#myElement2").toggleClass("myClass");
    });
    
    0 讨论(0)
提交回复
热议问题