metrical dashboard set sidebar auto open active by take current url and checking href using javascript

问题描述 投票:0回答:1

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");
  }
javascript html css sidebar
1个回答
0
投票

if
中对所有
for
使用
<a>

  for (allLiks){
     if (url == a.href){
       // add class to this a tag
     } 
  }

我给熟悉 JavaScript 基础知识的人一个笼统的答案。

© www.soinside.com 2019 - 2024. All rights reserved.