向适合 url 的子级添加类
我制作了一个单页布局网站,当用户来自 Google 或为其添加书签时,菜单不会显示活动/当前链接。
我制作了一个运行良好的脚本,但正如你所见......它看起来很可怕。我怎样才能做到这一点?
//Set .active to current page link
if (location.href.indexOf("#2") != -1) {
$("#menu li:nth-child(1) a ").addClass("active");
}
if (location.href.indexOf("#3") != -1) {
$("#menu li:nth-child(2) a ").addClass("active");
}
if (location.href.indexOf("#4") != -1) {
$("#menu li:nth-child(3) a ").addClass("active");
}
if (location.href.indexOf("#5") != -1) {
$("#menu li:nth-child(4) a ").addClass("active");
}
if (location.href.indexOf("#6") != -1) {
$("#menu li:nth-child(5) a ").addClass("active");
}
先感谢您...
I've made a one page layout website, where the menu doesn't show the active/current link, when users come from Google or bookmarked it.
I've made a script that works fine, but as you see.... it look horribly. How can I do this smart??
//Set .active to current page link
if (location.href.indexOf("#2") != -1) {
$("#menu li:nth-child(1) a ").addClass("active");
}
if (location.href.indexOf("#3") != -1) {
$("#menu li:nth-child(2) a ").addClass("active");
}
if (location.href.indexOf("#4") != -1) {
$("#menu li:nth-child(3) a ").addClass("active");
}
if (location.href.indexOf("#5") != -1) {
$("#menu li:nth-child(4) a ").addClass("active");
}
if (location.href.indexOf("#6") != -1) {
$("#menu li:nth-child(5) a ").addClass("active");
}
Thank you in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样:
How about something like: