简体   繁体   English

如何使jQuery活动状态忽略大写或小写进行url检测

[英]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. 例如,假设您单击菜单选项卡之一,则处于活动状态,但如果尝试手动更改url,而没有大写,则它将不起作用。

How to make the script works so that case sensitivity is not considered when checking the href agains what's in the URL? 如何使脚本有效,以便在检查href网址中的内容时不考虑区分大小写?

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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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