jquery活动类到基于当前url的菜单项

Int*_*els 3 jquery

我有以下代码,如果菜单项url == current url,应该在菜单项中添加一个活动的css类:

$("#accordion a").each(function() 
{   
    if (this.href.search(window.location.hostname) != -1)
    {
        $(this).addClass("active-sidebar-link");
    }
});
Run Code Online (Sandbox Code Playgroud)

但是这会将类添加到所有菜单项中.有小费吗?

und*_*ned 12

试试这个:

$("#accordion a").each(function() {   
    if (this.href == window.location.href) {
        $(this).addClass("active-sidebar-link");
    }
});
Run Code Online (Sandbox Code Playgroud)