检查当前url是否与字符串匹配

JRO*_*ROB 3 javascript

我试图查看菜单中是否存在指向当前页面URL的链接.

var thisUrl = window.location.href;

$("ul.leftmenu li a").each(function() { 

  var thisHref = $(this).attr('href');

  if (thisUrl === thisHref) { 

    // matches

  }

});
Run Code Online (Sandbox Code Playgroud)

只要当前窗口位置URL不包含任何_GET变量,此方法就可以正常工作.我如何修改它以忽略当前地址中的_GET变量?

dfs*_*fsq 6

您可以从中删除查询字符串部分href.例如这样:

var thisUrl = window.location.href.replace(location.search, '');
Run Code Online (Sandbox Code Playgroud)

location.searchproperty表示GET参数substring,即?param=value&param2=etc.