Detect if a link has been clicked and apply a different CSS class if it has

前端 未结 5 1818
轻奢々
轻奢々 2021-01-14 12:53

So what I want to do is determine if the current link is \'active\', i.e. if this link was just clicked by the user.

If it is, then I want to apply the class=s

5条回答
  •  别那么骄傲
    2021-01-14 13:28

    jQuery like this should work for you

    $(function(){
      $("a").click(function(){
        $("a").removeClass("selected")
        $(this).addClass("selected")
        return false;
      })
    })
    

    If you want this to work for a very specific set of links that add more to the selectors.

    For example let's say the ul has an id header

     
    

    then your jQuery should look like

    $(function(){
      $("#header a").click(function(){
        $("#header a").removeClass("selected")
        $(this).addClass("selected")
        return false;
      })
    })
    

提交回复
热议问题