Bootstrap导航菜单点击后无法自动添加active的处理方法

Hana ·
更新时间:2024-09-21
· 514 次阅读

Bootstrap提供了很丰富的前后端框架,为不精通CSS的程序猿们提供了很大的便利。前段时间在使用Bootstrap中的菜单控件时,其中的链接点击后,无法自动添加active类,即无法自动激活。需要适当做如下处理才OK。

废话说了,直接上代码:

<ul class="tabbable faq-tabbable"> <li class="active"><a href="@Url.Action(" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" Index", "MyContract", new { area = "MyData" })">我的合同</a></li> <li><a href="@Url.Action(" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" Index", "MyCashout", new { area = "MyData" })">我的请款</a></li> <li><a href="@Url.Action(" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" Index", "MyReceive", new { area = "MyData" })">我的入库</a></li> <li><a href="@Url.Action(" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" Index", "MyCashback", new { area = "MyData" })">我的付款</a></li> </ul>

这是一个典型的导航菜单,现在添加如下的脚本处理:   

$(function () { $(".faq-tabbable").find("li").each(function () { var a = $(this).find("a:first")[0]; if ($(a).attr("href") === location.pathname) { $(this).addClass("active"); } else { $(this).removeClass("active"); } }); })

原理很简单,就是找到所有的li标签,对其a标签的链接地址进行判断,如何和当前浏览器的地址一致,就认为是当前应该激活的菜单,添加active类,否则就取消。

如此,即可~~

以上这篇Bootstrap导航菜单点击后无法自动添加active的处理方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。



active 自动 菜单 bootstrap 方法

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章