添加“.active”类到使用jQuery或PHP的菜单中的当前页面的链接 [英] Add ".active" class to the current page's link in a menu using jQuery or PHP

查看:91
本文介绍了添加“.active”类到使用jQuery或PHP的菜单中的当前页面的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在页面上的活动任务菜单链接上添加 .active 类。我在localhost上工作,这里有几个例子URL:

I need to add .active class on active task's menu link on the page. I am working on localhost, here are few example URLs:

http://localhost/webapp/index.php?task=validate
http://localhost/webapp/index.php?task=register

该菜单的结构:

    <div class="menu">
       <ul class="sf-menu">
          <li><a href="index.php">Inicio</a></li>
          <li><a href="?task=validator">Validar</a></li>
          <li><a href="?task=register">Registro</a></li>
       </ul>
    </div>


推荐答案

您可以使用jQuery循环浏览所有链接,他们的网址 location 。下面是一个示例:

You can use jQuery to loop through all links and compare their URLs with location. Here is an example:

$(".sf-menu a").filter(function(){
    return this.href == location.href.replace(/#.*/, "");
}).addClass("active");

这篇关于添加“.active”类到使用jQuery或PHP的菜单中的当前页面的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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