Bootstrap导航栏活动类 [英] Bootstrap navbar active class

查看:76
本文介绍了Bootstrap导航栏活动类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用jquery从导航中初始化活动类,但是到目前为止,只有在url中的菜单项之后没有其他内容时,它才有效.

I try to use jquery to initialize active class from navigation, but so far it works only if there's nothing else after the menu item in url.

例如,如果我继续使用用户site.com/users,则该代码有效,但是当我浏览控制器并为其提供参数时,它会自动消失site.com/users/param1/param2

For ex, if i go on users site.com/users the code works, but when i browse the controller and i give it parameters it disappears ex site.com/users/param1/param2

下面您可以看到我的代码:

below you can see my code:

$(document).ready(function () {
    var url = window.location;
    // Will only work if string in href matches with location
    $('ul.nav a[href="' + url + '"]').parent().addClass('active');

    // Will also work for relative and absolute hrefs
    $('ul.nav a').filter(function () {
        return this.href == url;
    }).parent().addClass('active').parent().parent().addClass('active');
});

推荐答案

尝试一下:

$('ul.nav a').filter(function () {
        return url.href.indexOf(this.href) >= 0;
    }).parent().addClass('active').parent().parent().addClass('active');

这篇关于Bootstrap导航栏活动类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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