简体   繁体   中英

How to make jQuery active state ignore uppercase or lowercase for url detection

This is my code to make active state working, the probleme is it's case sensitive. Example, let's say you click on one of the menu tab, you get the active state working but if you try to change manually the url to not have let's say uppercase then it won't work.

How to make the script works so that case sensitivity is not considered when checking the href agains what's in the URL?

Thanks all!

$(document).ready(function() {
    $("ul#menu li a").wrapInner("<span></span>");
    $("ul#menu li a span").css({
        "opacity": 0
    });
$("ul#menu li a").hover(function() {
    $(this).children("span").animate({
        "opacity": 1
    }, 400);
}, function() {
    $(this).children("span").animate({
        "opacity": 0
    }, 400);
});
}); /**ACTIVE STATE SCRIPT**/
$(function() {
    var pathname = window.location.pathname;
    var page = pathname.match(/\/([^\/]+\.[^\/]+)/i)[1];
    var target = $('#menu a').filter(function() {
    console.log($(this).attr('href'), page);
    return $(this).attr('href').toLowerCase().indexOf(page.toLowerCase()) > -1;
});
    $(target).addClass('active');
});

First of, change your regexp to that :

var page = pathname.match(/\/([^\/]+\.[^\/]+)/i)[1];

Then your selector :

var target = $('#menu a').filter(function(){
    return $(this).attr('href').toLowerCase().indexOf(page.toLowerCase()) > -1;
});

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