jquery add remove class onclick

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

i have any navigation menu !

   
  • HOME
3条回答
  •  [愿得一人]
    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.

提交回复
热议问题