在菜单中使用jquery突出显示当前网址 [英] Highlight current url in menu ith jquery

查看:79
本文介绍了在菜单中使用jquery突出显示当前网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个菜单,但我想突出显示当前与jquery的链接。

I have a menu but I want to highlight current link with jquery.

var loc = window.location;
   var lochref = $("#topNavigation li a").attr("href");

   if(lochref == loc){
       $('#topNavigation li a').addClass('currenthover');
   }

使用上述脚本,a类没有任何变化。我怎么能用jquery做到这一点?
提前致谢

With above script, there is no any change in "a" class. How can I do this with jquery? Thanks in advance

推荐答案

首先:这是您应该考虑做服务器端的事情。它更加简单和可靠。

First off: This is something you should consider doing server side. It's much, much, much more simpler and reliable.

您的代码仅比较第一个链接的href,因为 attr 返回第一个链接的属性值。您需要遍历链接以找到正确的链接。

Your code compares only the href of the first link, as attr returns the property value of the first link. You need to loop over the links to find the right one.

var loc = window.location.href;
$("#topNavigation li a").each(function() {
  if(this.href == loc) {
     $(this).addClass('currenthover');
  }
});

这篇关于在菜单中使用jquery突出显示当前网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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