使用jQuery或PHP将".active"类添加到菜单中的当前页面链接

Jos*_*ias 1 css php jquery

我需要.active在页面上的活动任务菜单链接上添加类.我正在使用localhost,这里有几个示例网址:

http://localhost/webapp/index.php?task=validate
http://localhost/webapp/index.php?task=register
Run Code Online (Sandbox Code Playgroud)

和菜单的结构:

    <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>
Run Code Online (Sandbox Code Playgroud)

Sal*_*n A 15

您可以使用jQuery循环遍历所有链接并将其URL与之进行比较location.这是一个例子:

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