[英]Need help adding and removing .active class based on page URL using JS
I want to add .active class to <a>
tag depending on which page I'm currently on, so I can style it.我想根据我当前所在的页面将 .active 类添加到
<a>
标记,以便我可以对其进行样式设置。
When I go to Home page, this should happen:当我转到主页时,应该会发生这种情况:
<li><a class="active" href="#">HOME</a></li>
<li><a href="#">ABOUT</a></li>
<li><a href="#">LOCATION</a></li>
When I go to About page, then this should happen:当我转到关于页面时,应该会发生这种情况:
<li><a href="#">HOME</a></li>
<li><a class="active" href="#">ABOUT</a></li>
<li><a href="#">LOCATION</a></li>
Etcetra...等等...
This image describes what I'm trying to achieve:这张图片描述了我想要实现的目标:
const currentLocation = location.href; const menuItem = document.querySelectorAll('a'); const menuLength = menuItem.length for (let i = 0; i<menuLength; i++){ if(menuitem[i].href === currentLocation){ menuItem[i].className = "active" } }
ul li a { color: black; background-color: silver; } ul li a.active { color: white; background-color: black; }
<nav class="navbar"> <div class="navbar-links"> <ul> <li><a href="http://example.com/home/">HOME</a></li> <li><a href="http://example.com/about/">ABOUT</a></li> <li><a href="http://example.com/location/">LOCATION</a></li> </ul> </div> </nav>
It just doesn't work.它只是不起作用。 Any suggestions?
有什么建议?
Target only the .navbar-links a
then loop over each one, in the loop do 2 things:仅定位
.navbar-links a
然后循环遍历每个,在循环中做两件事:
hash
or path
equals the links href
attribute.hash
或path
等于链接href
属性。 const menuItem = document.querySelectorAll('.navbar-links a'); menuItem.forEach(el => { // current if (el.getAttribute('href') === (location.hash || '#home')) { el.classList.add("active") } // handle click el.addEventListener("click", e => { // remove others menuItem.forEach(el => el.classList.remove("active")) // set active e.target.classList.add("active") }) })
ul li a { color: black; background-color: silver; } ul li a.active { color: white; background-color: black; }
<nav class="navbar"> <div class="navbar-links"> <ul> <li><a href="#home">HOME</a></li> <li><a href="#about">ABOUT</a></li> <li><a href="#location">LOCATION</a></li> </ul> </div> </nav>
If not using a hash the extra steps are not needed as it will reload the page:如果不使用哈希,则不需要额外的步骤,因为它会重新加载页面:
const menuItem = document.querySelectorAll('.navbar-links a'); menuItem.forEach(el => { if (el.getAttribute('href') === (location.path || '/home')) { el.classList.add("active") } })
ul li a { color: black; background-color: silver; } ul li a.active { color: white; background-color: black; }
<nav class="navbar"> <div class="navbar-links"> <ul> <li><a href="/home">HOME</a></li> <li><a href="/about">ABOUT</a></li> <li><a href="/location">LOCATION</a></li> </ul> </div> </nav>
location.href will return the full url. location.href 将返回完整的 url。
I think what you want is location.pathname, so if you have something like www.yourdomain.com/home location.pathname would be equals to "/home" while location.href would be equals to www.yourdomain.com/home .我认为你想要的是 location.pathname,所以如果你有类似www.yourdomain.com/home 的东西,location.pathname 将等于 "/home" 而 location.href 将等于www.yourdomain.com/home 。
Also, all a tags are pointing to the same '#'.此外,所有 a 标签都指向相同的“#”。
What you want to do is probably set the href to the path you want.您想要做的可能是将 href 设置为您想要的路径。
for ex:例如:
<li><a href="/home">HOME</a></li>
Here is the javascript you can use:这是您可以使用的javascript:
const menuItem = document.querySelectorAll('a');
menuItem.forEach( item => {
if(item.href===location.href){
item.classList.add('active')
return;
}
})
I used foreach array method to loop through all links.我使用 foreach 数组方法遍历所有链接。 In the if statement, we are confirming if item.href (href attribute of a tag) is equal to current url you are on, if that is true, it will add class of active to that link and return.
在 if 语句中,我们正在确认 item.href(标签的 href 属性)是否等于您所在的当前 url,如果为真,它将向该链接添加 active 类并返回。
Not sure if that's what u wanted i am also a beginner!不知道这是否是你想要的,我也是初学者!
This solved my problem:这解决了我的问题:
const menuItem = document.querySelectorAll('.navbar-links a'); menuItem.forEach(el => { // current if (el.getAttribute('href') === (location.href || 'http://example.com/home/')) { el.classList.add("active") } // handle click el.addEventListener("click", e => { // remove others menuItem.forEach(el => el.classList.remove("active")) // set active e.target.classList.add("active") }) })
.navbar-links a { color: black; background-color: silver; } .navbar-links a.active { color: white; background-color: black; }
<nav class="navbar"> <div class="navbar-links"> <ul> <li><a href="http://example.com/home/">HOME</a></li> <li><a href="http://example.com/about/">ABOUT</a></li> <li><a href="http://example.com/location/">LOCATION</a></li> </ul> </div> </nav>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.