提问者:小点点

检查href是否是网站路径之后的路径


我想检查一个链接的href是否是网站路径之后的文件。代码如下:

const currentLocation = location.href;
const menuItems = document.querySelectorAll('nav ul li a');

$('nav ul li a').each(index => {
    if(menuItems[index].href === currentLocation || (menuItems[index].href === 'http://127.0.0.1:5500/' && currentLocation === 'http://127.0.0.1:5500/index.html')) {
        // other code
    }
});

有没有办法让它检查在“http://127.0.0.1:5500”后面是“/”还是“/index.html”?谢谢你!


共1个答案

匿名用户

您可以使用endsWith进行检查。

此外,如果需要,可以直接检查(MenuItems[index].href(MenuItems[index].pathname)而不是MenuItems[index].href