Javascript 检查href是否在网站路径之后
我想检查链接的href是否是网站路径后的文件。代码如下:Javascript 检查href是否在网站路径之后,javascript,jquery,Javascript,Jquery,我想检查链接的href是否是网站路径后的文件。代码如下: const currentLocation=location.href; const menuItems=document.queryselectoral('nav ul li a'); $('nav ul li a')。每个(索引=>{ if(menuItems[index].href==currentLocation | |(menuItems[index].href==http://127.0.0.1:5500/“&¤t
const currentLocation=location.href;
const menuItems=document.queryselectoral('nav ul li a');
$('nav ul li a')。每个(索引=>{
if(menuItems[index].href==currentLocation | |(menuItems[index].href==http://127.0.0.1:5500/“&¤tLocation==”http://127.0.0.1:5500/index.html')) {
//其他代码
}
});
有没有办法让它检查在'http://127.0.0.1:5500'? 谢谢大家! 您可以使用endsWith进行检查
也可以不使用此
menuItems[index].href
直接检查(menuItems[index].pathname
。您可以使用endsWith
进行检查。谢谢!这很有效。这是更新后的代码:$('nav ul li a')。每个(index=>{if(menuItems[index])。href==currentLocation | | |(menuItems[index].href.endsWith('/')&¤tLocation.endsWith('/index.html')){//other code}});也许你可以把这个作为答案,然后我就可以接受了。另外,你是如何在注释中放置多行代码的?很高兴我能帮上忙!