jquery add remove class

前端 未结 2 408
心在旅途
心在旅途 2020-12-15 07:21

I have 4 anchors, I want to add a class of current to an anchor as it is clicked and remove the class from the other 3 at the same time. Here\'s my code. what am I doing wro

相关标签:
2条回答
  • 2020-12-15 08:05

    I think this will help you

    $("a").click(function() {
      $('a').removeClass('current');
      var id = $(this).attr('id');
      $('#'+id).addClass('current');
    });
    
    0 讨论(0)
  • 2020-12-15 08:08

    By using event-delegation, we only bind one handler for all click events. When an anchor (other than the .current anchor) is clicked, we strip the .current anchor of its class, and make the clicked anchor the new current one:

    $("#thumbs").on("click", "a:not(.current)", function ( event ) {
        $(".current", event.delegateTarget).removeClass("current");
        $(this).addClass("current");
    });
    
    0 讨论(0)
提交回复
热议问题