jQuery活动类到基于当前URL的菜单项 [英] jquery active class to menu item based on current url

查看:69
本文介绍了jQuery活动类到基于当前URL的菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,如果菜单项url ==当前url,则应在菜单项中添加一个活动的CSS类:

I have the following code that should add an active css class to the menu item if menu item url == current url:

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

但是这会将类添加到所有菜单项中.有什么提示吗?

but this adds the class to all the menu items. any tips?

推荐答案

尝试一下:

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

这篇关于jQuery活动类到基于当前URL的菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆