简体   繁体   English

如果 URL 包含 VALUE 添加类到链接 + ROOT

[英]If the URL contains VALUE add class to link + ROOT

I would like to add an active link to my navigation depending on the url using vanilla JS.我想根据使用 vanilla JS 的 url 添加到我的导航的活动链接。

My navigation looks like this:我的导航是这样的:

 <div id="navi">
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/blog">Blog</a></li>
    <li><a href="/contact">Contact</a></li>
 </ul>
</div>

JS looks like this: JS 看起来像这样:

function Activelink() {
 link = document.getElementById('navi').getElementsByTagName('a');
 for(i=0; i<link.length; i++) {
    if(document.location.href.indexOf(link[i].href) != -1) {
     link[i].className='active';}
 }}
window.onload = Activelink;

Works well, especially as it also adds the class "active" to the blog link even if a blog post is accessed.效果很好,特别是因为即使访问了博客文章,它也会向博客链接添加“活动”类。 Only issue is that <a href="/">Home</a> permanently get's the class "active" assigned as well.唯一的问题是<a href="/">Home</a>永久获得了分配的“活动”类。 Is there a way to avoid this, so that the home link class is only "active" if the homepage is accessed?有没有办法避免这种情况,以便主页链接类只有在访问主页时才“活动”?

You can use endsWith instead of indexOf :您可以使用endsWith而不是indexOf

function Activelink() {
 link = document.getElementById('navi').getElementsByTagName('a');
 for(i=0; i<link.length; i++) {
    if(document.location.href.endsWith(link[i].href)) {
     link[i].className='active';}
 }}
window.onload = Activelink;

First of all, welcome to stack overflow!首先,欢迎堆栈溢出! The answer depends a bit on your future concept of the site and if you will have URLs that are not in your navigation.答案在一定程度上取决于您对网站的未来概念,以及您是否将拥有不在导航中的 URL。 For example, if you have some kind of copyright page accessible at /copyright linked in the footer but not in your navigation, what should be highlighted then?例如,如果您在页脚中链接的 /copyright 处有某种版权页面可访问,但在导航中没有,那么应该突出显示什么? The Home entry?主页条目? No entry?禁止入内? Furthermore I assume /blog and /contact can also have subpages?此外,我假设 /blog 和 /contact 也可以有子页面? Like /blog/what-ever?喜欢/博客/什么?

So regardless of the corner cases in the future, a simple solution would be to reverse the for-loop and add a break to the if clause: (Note I changed /blog to /js, because that is part of the URL this test snippet runs in.)因此,不管未来的极端情况如何,一个简单的解决方案是反转 for 循环并在 if 子句中添加一个中断:(注意我将 /blog 更改为 /js,因为这是此测试片段的 URL 的一部分跑进来。)

 function activeLink() { link = document.getElementById('navi').getElementsByTagName('a'); for(i = link.length - 1; i >= 0; i--) { if(document.location.href.indexOf(link[i].href) != -1) { link[i].className='active'; break; } }} window.onload = activeLink;
 a.active{ background-color:red; }
 <div id="navi"> <ul> <li><a href="/">Home</a></li> <li><a href="/js">Blog</a></li> <li><a href="/contact">Contact</a></li> </ul> </div>

Now your for loop checks if window.location.href contains /contact , then if it matches the break would skip all further executions of your loop.现在您的 for 循环检查window.location.href包含/contact ,然后如果它匹配 break 将跳过循环的所有进一步执行。 Otherwise it will search for /blog and only if none of the first two executions matches it would pick / , as that will always be part of the URL.否则它将搜索/blog并且只有在前两次执行中没有一个匹配时才会选择/ ,因为它始终是 URL 的一部分。 Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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