Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查href是否在网站路径之后_Javascript_Jquery - Fatal编程技术网

Javascript 检查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

我想检查链接的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/“&¤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}});也许你可以把这个作为答案,然后我就可以接受了。另外,你是如何在注释中放置多行代码的?很高兴我能帮上忙!