设置活动链接 jQuery
我的网站上有这样的链接结构...
index.php - homepage
index.php?section= - other pages
这是我编写的一段代码,用于更改活动链接的颜色...
$(function(){
var path = location.pathname.substring(1);
if ( path )
$('#nav ul#navigation li a[href$="' + path + '"]').attr('class', 'selected');
});
但这没有考虑 PHP 链接结构,仅考虑平面文件链接。我如何才能使像 index.php?section=
这样的链接以及像 index.php
这样的基本链接发挥作用?
I have a link structure like this on my website...
index.php - homepage
index.php?section= - other pages
here is a piece of code that I wrote to change the colour of the active link...
$(function(){
var path = location.pathname.substring(1);
if ( path )
$('#nav ul#navigation li a[href$="' + path + '"]').attr('class', 'selected');
});
but this doesn't take into account of PHP link structures, only flat file links. How would I get this working for links like index.php?section=
as well as the basic links like index.php
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用类名而不是完整的 URL。这让它变得不那么啰嗦。
然后在你的html中:
如果你想减少对url的依赖,你可以使用散列:
URL: http://yoursite.com?blabblah=xxx....#section1
然后使用 document.location.hash (这将 = "section1")作为部分指示符。它节省了您解析 URL 的步骤。
Use class names instead of the full URL. It makes it a bit less wordy.
Then in your html:
If you want be a bit less url-dependent, you can use a hash:
URL: http://yoursite.com?blabblah=xxx....#section1
Then use document.location.hash (which will = "section1") as the section indicator. It saves you the step of parsing the URL.
您可以尝试删除 index.php?section= 并使用 contains 选择器 (*=)。
You could try stripping out index.php?section= and use the contains selector (*=).