https://github.com/swapan730/metrical 这是我使用的仪表板
我需要在此仪表板的侧边栏中使用 javascript 以在页面加载期间检查页面 url 并自动将其设置为活动状态,但我很困惑。
我的代码如下
var url = window.location;
const allLinks = document.querySelectorAll('.nav-item a');
const currentLink = [...allLinks].filter(e => {
return e.href == url;
});
if (currentLink.length > 0) {
currentLink[0].classList.add("active");
}
在
if
中对所有for
使用<a>
。
for (allLiks){
if (url == a.href){
// add class to this a tag
}
}
我给熟悉 JavaScript 基础知识的人一个笼统的答案。