使用 jQuery 动态突出显示我的导航栏链接的最佳替代方案?
我有一个 ASP.NET 网站,在母版页上有一个基本的导航菜单:
<ul id="menu">
<li><a href="Default.aspx"><span>Home</span></a></li>
<li><a href="Services.aspx"><span>Services</span></a></li>
<li><a href="HowItWorks.aspx"><span>How It Works</span></a></li>
<li><a href="ContactUs.aspx"><span>Contact Us</span></a></li>
</ul>
我的目标是在母版页上添加一个 JavaScript 函数,该函数将动态地将“活动”类添加到表示用户所在页面的标记中。例如,如果用户位于 www.mywebsite.com/default.aspx,则应将属性 [class='active'] 添加到导航栏中的第一个链接。活动类基本上只是突出显示链接,以便用户知道他们当前所在的页面。
这是我的 JavaScript 代码:
<script type="text/javascript">
$(document).ready(selectMenuItem);
function selectMenuItem() {
var name = getPageName();
if (name)
$('#menu a[href="' + name + '"]').addClass("active");
}
function getPageName() {
var path = window.location.href.split('?')[0];
var name = path.substring(path.lastIndexOf('/') + 1, path.length);
return name;
}
</script>
当用户访问网站上的任何页面时,会执行 selectMenuItem() 函数。该函数调用 getPageName(),返回用户正在查看的页面文件的名称(例如:“default.aspx”)。该函数使用此名称字符串来查找哪个标签链接到用户当前所在的页面,当找到该类时,该类已正确添加。
该代码运行良好,但该过程区分大小写。基本上我任由用户摆布。如果他们手动输入“mywebsite.com/DefUlT.aspx”,那么我的代码将失败,因为子字符串“default.aspx”中的大小写与标记中的大小写不匹配。我可以通过告诉 JavaScript 函数强制名称字符串为小写来解决这个问题,但这意味着每当我添加新的菜单项时,我需要确保将 href 属性全部写为小写,这不是很重要,但对我来说还不够优雅。如果我是这个网站上唯一的工作人员,那不会有问题,但我会将工作交给其他许多专业人士,我觉得告诉每个人很愚蠢,“确保 href 值全部小写或代码会断的!”
对挑战感兴趣的人可以告诉我如何解决这个问题吗?我对 jQuery 非常陌生(昨晚开始),所以我确信有一个优雅的解决方案,涉及更改这行代码:
$('#menu a[href="' + name + '"]').addClass (“积极的”);
我的问题是,在与 name 变量进行比较之前,我需要将“href”属性转换为小写,然后我只需执行“name.toLowerCase()”,以便我知道 href 属性之间的大小写差异和名称变量无关。是否有一个等效的 jQuery 选择器允许我指定一个表达式?
谢谢,
-安德鲁·C.
I have an ASP.NET website, on the masterpage I have a basic navigation menu:
<ul id="menu">
<li><a href="Default.aspx"><span>Home</span></a></li>
<li><a href="Services.aspx"><span>Services</span></a></li>
<li><a href="HowItWorks.aspx"><span>How It Works</span></a></li>
<li><a href="ContactUs.aspx"><span>Contact Us</span></a></li>
</ul>
My goal is to add a JavaScript function on the masterpage that will dynamically add the "active" class to the tag that represents the page the user is on. For example, if the user is on www.mywebsite.com/default.aspx, than the attribute [class='active'] should be added to the first link in the navbar. The active class basically just highlights the link so the user knows which page they are currently on.
Here is my JavaScript code:
<script type="text/javascript">
$(document).ready(selectMenuItem);
function selectMenuItem() {
var name = getPageName();
if (name)
$('#menu a[href="' + name + '"]').addClass("active");
}
function getPageName() {
var path = window.location.href.split('?')[0];
var name = path.substring(path.lastIndexOf('/') + 1, path.length);
return name;
}
</script>
Here is what happens, when the user visits any page on the site, the selectMenuItem() function executes. The function makes a call to getPageName() which returns the name of the page file that the user is viewing (ex: "default.aspx"). The function uses this name string to find which tag links to the page the user is currently on, when found the class is correctly added.
This code works great, however the process is case sensitive. Basically I'm at the mercy of the user. If they manually type in "mywebsite.com/DefUlT.aspx" than my code will fail because the casing in the substring "default.aspx" does not match the one in the tag. I could solve this problem by telling the JavaScript function to force the name string to be lower case, however this would mean that whenever I add new menu item I need to be sure I write the href attribute all in lower case, which isn't a big deal but it's not elegant enough for me. It wouldn't be a problem if I was the only one working on this site, but I will hand the work of to a number of other professionals and I feel stupid telling everyone, "be sure the href values are all lowercase or the code will break!"
Can someone interested in a challenge maybe tell me how to work around this? I'm extremely new to jQuery (started last night) so I'm sure there's an elegant solution that involes changing this line of code:
$('#menu a[href="' + name + '"]').addClass("active");
My problem is that I need the "href" attribute to be turned to lower case before the comparison is made to the name variable, I would then just perform "name.toLowerCase()" so that I know the case differences between the href attribute and name variable are irrelevant. Is there an equivalent jQuery selector which allows me to specify an expression perhaps?
Thanks,
-Andrew C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
.filter()
try this with
.filter()
我不知道如何使用选择器来做到这一点,但是循环遍历所有菜单链接,如下所示,您可以将
toLowerCase()
方法应用于 href 和名称,所以无论用户在 URL 中输入什么内容,您都会知道匹配会起作用:I don't know of a way to do it with a selector, but looping through all your menu links as in the following, you could apply the
toLowerCase()
method to both the href and name, so you'd know the match would work, no matter what the user entered in the URL: