Jquery if link = page url

ok is pretty simple but i don't know how ...

I just want to make an active state (maybe just make it bold)

My menu - ul-li

I can't figure out how to write this, if the URL matches one of the links, make the link bold.

please, help

thank you for your time

+3
source share
2 answers

Example: http://jsfiddle.net/patrick_dw/NYQnP/2/

$('ul > li a[href$=' + window.location.pathname + ']').css('font-weight','bold');

Or maybe better like this, which makes exact matching of both attributes pathname:

$('ul > li a[href]').filter(function() {
    return this.href.pathname === window.location.pathname;
).css('font-weight','bold');

If you use the full domain in href, you can change it to:

return this.href === window.location;
+8
source

, :

$(function(){
       $("a").each(function(){
               if ($(this).attr("href") == window.location.pathname){
                       $(this).addClass("selected");
               }
       });
});

- https://css-tricks.com/snippets/jquery/highlight-all-links-to-current-page/

+1

Source: https://habr.com/ru/post/1780276/


All Articles