我想检查链接的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
}
});
有没有办法让它检查"后面是"/"还是"/index.html"http://127.0.0.1:5500"?非常感谢。
您可以使用endsWith进行检查。
如果您愿意,也可以直接检查(menuItems[index].pathname
,而不是这个menuItems[index].href
。