jquery if link = page url

Ale*_*lex 5 javascript css url jquery href

好的很简单,但我不知道如何......

我只想做一个活跃的状态(可能只是让它变粗)

我的菜单是ul-li

我无法弄清楚如何编写它,所以如果网址与其中一个链接匹配,请将链接设为粗体

请帮忙

谢谢你的时间

use*_*716 12

示例: http ://jsfiddle.net/patrick_dw/NYQnP/2/

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

或者更像这样,它可以完全匹配两个pathname属性:

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

如果您使用的是完整域名,则href可以将其更改为:

return this.href === window.location;
Run Code Online (Sandbox Code Playgroud)

  • `返回 this.href === window.location.href;` (2认同)