Javascript 如果类的href包含在window href中,如何将其添加到header元素?

Javascript 如果类的href包含在window href中,如何将其添加到header元素?,javascript,html,jquery,href,Javascript,Html,Jquery,Href,如果类的href是window.location.href的索引,则我的当前代码将类应用于适当的头元素 $(function() { $('.sidebar-links li.active').removeClass("active"); $('.sidebar-links li a').filter(function(){ return this.href.indexOf(window.location.href) !== -1; }).

如果类的
href
window.location.href
索引,则我的当前代码将类应用于适当的头元素

$(function() {
    $('.sidebar-links li.active').removeClass("active");
    $('.sidebar-links li a').filter(function(){
        return this.href.indexOf(window.location.href) !== -1;
            }).parent().addClass("active");
});
标题href的示例是

如果我的
window.location.href
与此完全匹配,则效果非常好。我遇到的问题是,如果我单击某个生成查询字符串的内容,如

然后刷新页面,header元素不再具有该类。
是否可以更改函数,以便在
窗口中包含header元素的href.location.href
时添加该类?或者根本没有查询字符串?


您可以使用
window.location.href.split(“?”)[0]
进行快速破解

如果你想要一个更别致的解决方案

$('.sidebar links li.active').removeClass(“active”);
$('.sidebar链接li a').filter(函数(){
返回此.href.indexOf(window.location.href.split('?')[0])!=-1;
}).parent().addClass(“活动”);

您可以使用以下命令删除URL参数:
URL.replace(/\?*/,“”)

在代码中,更改
this.href.indexOf(window.location.href)!==-1
this.href.indexOf(window.location.href.replace(/\?*/,'')!=-1