简体   繁体   中英

How to change the style of an active anchor (in a sidebar nav)?

I am trying to change the style of a sidebar nav anchor when the user scrolls and hovers over the section relevant to that particular anchor (bootstrap scrollspy). that anchor has 2 classes by default: nav-link and js-scroll-trigger", but when we scroll to a particular section, I see another class added to the anchor: "active". so How can I change the style of the anchor text (for ex make it dark), using JS? I already did this code, but it is not working:

const activeNavLink = document.querySelector(".nav-link")
const activeNav = document.querySelector(".active")
experience.addEventListener("mouseover", () => {
 if (active)
activeNav.style.color= "black"
}

experience is the section wrapping the content.

you can add another class and write their color: black; then it will override the bootstrap class

Check this link CheckThis . You can't access that class name maybe you can style it by CSS. If that hyperlink is active set the color as dark

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM