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

前端 未结 5 1819
轻奢々
轻奢々 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:32

    Bonkydog already gave you this hint in his comment, here it is as a working Sinatra example (using HAML instead of ERB though):

    require 'sinatra'
    require 'haml'
    
    get '/bar' do
      @urls = ['/foo', '/bar', '/baz']
      haml :index
    end
    
    __END__
    
    @@ index
    %ul
      -@urls.each do |url|
        %li{:class => request.path_info == url ? 'selected' : nil}
          %a{:href => url}
            =url
    

    Edit: After a clarification in the comment, turns out that this seems to be pure HTML/CSS problem. Check out :target pseudo selector:

    
      
        
      
      
        
      
    
    

    Not sure whether some browsers support it though.

提交回复
热议问题