简体   繁体   中英

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/. 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. Now, how would I strip out the filename.html from the link and just get the directory? 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 ******/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM