Active Menu Highlight CSS

后端 未结 11 898
孤独总比滥情好
孤独总比滥情好 2020-11-29 00:54

I want to highlight the current menu you have click. I\'m using CSS, but it is now working.

here is my css code:

#sub-header ul li:hover{ background-         


        
11条回答
  •  情歌与酒
    2020-11-29 01:47

    Let's say we have a menu like this:

    
    

    Let our current url be https://demosite.com/link1.html

    With the following function we can add the active class to which menu's href is in our url.

    let currentURL = window.location.href;
    
    document.querySelectorAll(".menu a").forEach(p => {
      if(currentURL.indexOf(p.getAttribute("href")) !== -1){
        p.classList.add("active");
      }
    })
    

提交回复
热议问题