[英]stripping file.html out of the link
So, I am writing a script that is suppose to illuminate different sections of the nav. 因此,我正在编写一个脚本,以阐明导航的不同部分。 each nav link is set up to be /directory/subdir/. 每个导航链接都设置为/ directory / subdir /。 It worked until I realized there needs to be more to this. 它一直起作用,直到我意识到需要做更多的事情为止。 if you are in a directory that is not the index file it doesn't work since it was directly matching HREF attribute. 如果您所在的目录不是索引文件,则该目录不起作用,因为它与HREF属性直接匹配。 Now, how would I strip out the filename.html from the link and just get the directory? 现在,我如何从链接中删除filename.html并仅获取目录? Thanks for all your help contributors! 感谢您提供的所有帮助!
If i don't misunderstand, you're trying to hilight the your menu's link with 如果我没有误会,您正在尝试突出显示菜单链接
href="/dir/abc/"
while the page address is something like : 而页面地址类似于:
/dir/abc/index.shtml
/dir/abc/detail.shtml
/dir/abc/etc.shtml
right ? 对 ? I've edited your code a bit. 我已经对您的代码进行了一些编辑。 Hope this help : 希望这个帮助:
var loc = document.location.pathname;
var nav = $('.mainNav a');
nav.each(function(index, element) {
var href = $(this).attr('href') //-- edited
if (loc.indexOf(href)==0) { //-- edited
var node = nav.eq(index).parent('li');
var gpnode = node.parents('li');
node.addClass('active');
gpnode.addClass('active');
}
});
/****** LINK DETECTION ******/
var nav = $('.mainNav a');
var loc = document.location.pathname;
var pathname = document.location.pathname.substring(1);
var parts = pathname.split(/\//);
var x = parts.length;
var hrefStr = "/"+parts[0]+"/"+parts[1]+"/";
switch (x) {
case 2:
$('.mainNav a').each(function(index, element) {
if ($(this).attr('href') === loc) {
var node = nav.eq(index).parent('li');
var gpnode = node.parents('li');
node.addClass('active');
gpnode.addClass('active');
}
});
break;
default:
$('.mainNav a').each(function(index, element) {
if ($(this).attr('href') === hrefStr) {
var node = nav.eq(index).parent('li');
var gpnode = node.parents('li');
node.addClass('active');
gpnode.addClass('active');
}
});
}
/****** END LINK DETECTION ******/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.