2017-04-08 15 views
1

想法:我想添加class =“active”到li元素,当特定的链接被点击,这是相当于href和url。Javascript获取从子元素(无jquery)剥离href值分配活动类

问题:我无法选择嵌套在ul里面的href值。 CSS可能会尽早加载样式。 注意:html被注入了php。

代码:

window.onload = function() { 
 
\t 
 
var curURL = window.location.href.split("/").pop(); //Outputs the current page url ex index.php 
 

 
var linkNow = document.getElementsByClassName("topnav").getElementsByTagName("a").href;//Nesting this does not work but how can it be achieved? 
 

 

 
//Loop check which curURL matches the href value, true->assign active class 
 

 
for (var i = 0; i < link.length; i++) { 
 
    if (curURL == linkNow[i]){ 
 
     linkNow.addClass("active"); 
 
     break; 
 
    } 
 
     
 
} 
 

 
//As soon as the a element has the class="active" the navbar color will change 
 
}
<ul class='topnav'> 
 
    <li><a href='index.php'>Home</a></li> 
 
    <li><a href='news.php'>News</a></li> 
 
    <li><a href='showcase.php'>Showcase</a></li> 
 
    <li class='icon'> 
 
    <a href='javascript:void(0);' onclick='myFunction()'>&#9000;</a><!-- responsive icon--> 
 
    </li> 
 
</ul>

+0

你不应该需要说 “不jQuery的”。没有标签,它在OP中没有使用,因此答案不应该使用它。 – RobG

+0

对不起。你看我研究了相当数量,并且大多数答案都是针对jQuery的,所以我想澄清;) –

回答

1

有多种方式去做,一个是使用直接获取元素的选择器,例如

window.onload = function() { 
 
    var href = 'index.php'; 
 
    var link = document.querySelector('a[href="' + href + '"]'); 
 
    link.parentNode.className = 'active'; 
 
}
.active { 
 
    background-color: red; 
 
}
<ul class='topnav'> 
 
    <li><a href='index.php'>Home</a></li> 
 
    <li><a href='news.php'>News</a></li> 
 
    <li><a href='showcase.php'>Showcase</a></li> 
 
</ul>

为了与旧的浏览器兼容,你也可以使用更广泛的选择,如:

var links = document.querySelectorAll('ul a'); 

甚至:

var links = document.links; 

这是兼容几乎每个浏览器都有,然后循环播放r他们寻找适当的价值。

+0

OP想要'li'类,而不是'a' – LGSon

+0

我喜欢''a [href =“'+ href +'”]'',这很明显(当你看到它的时候),但很少见到。 .. plus 1 – LGSon

+0

这也适用。所选代码中最短的。 –

0

queryselectorAll()简单的使用。这样document.querySelectorAll('.topnav li a')

  1. 在你的代码for loop length使用linkNow代替link
  2. 而与之搭配的适用,如果条件试试这个(curURL == linkNow[i].href)

window.onload = function() { 
 

 
    var curURL = window.location.href 
 
    var linkNow = document.querySelectorAll('.topnav li a') 
 
    for (var i = 0; i < linkNow.length; i++) { 
 
    if (curURL == linkNow[i].href) { 
 
     linkNow[i].classList.add("active"); 
 
     console.log(linkNow[i]) 
 
     break; 
 
    } 
 
    else{ 
 
    linkNow[i].classList.remove('active') 
 
    
 
    } 
 

 
    } 
 
}
.active{ 
 
color:red; 
 
}
<ul class='topnav'> 
 
    <li><a href='index.php'>Home</a></li> 
 
    <li><a href='news.php'>News</a></li> 
 
    <li><a href='js'>Its for test only</a></li> 
 
    
 
    <li><a href='showcase.php'>Showcase</a></li> 
 
    <li class='icon'> 
 
    <a href='javascript:void(0);' onclick='myFunction()'>&#9000;</a> 
 
    <!-- responsive icon--> 
 
    </li> 
 
</ul>

+0

OP希望在'li'类上,而不是在'a'上......我猜'.parentElement'会修复那 – LGSon

+0

我其实确实需要这个类在a上。当我显示站点的源时,没有任何类包含活动的。 –

+0

页面上的输出需要为:

  • Home
  • 然后我的CSS启动并着色链接。 –

    0

    这里的关键错误是getElementsByClassName返回数组。请注意功能名称中的s

    所以,如果你想使用这个功能,记得循环遍历结果。我只是使用[0]作为简单的例子。

    window.onload = function() { 
     
        var curURL = 'news.php'; 
     
    
     
        var lis = document.getElementsByClassName("topnav")[0].getElementsByTagName("li"); 
     
    
     
        for (var i = 0; i < lis.length; i++) { 
     
        var a = lis[i].getElementsByTagName("a")[0]; 
     
        if (a.getAttribute("href") === curURL) { // using .getAttribute() to get raw value instead of full url 
     
         //lis[i].classList.add("active"); //use this instead if you want to add class to <li> 
     
         a.classList.add("active"); // be careful .addClass() is an jquery function 
     
         break; 
     
        } 
     
    
     
        } 
     
    }
    li.active { 
     
        background: red 
     
    } 
     
    
     
    a.active{ 
     
        color: yellow 
     
    }
    <ul class='topnav'> 
     
        <li><a href='index.php'>Home</a></li> 
     
        <li><a href='news.php'>News</a></li> 
     
        <li><a href='showcase.php'>Showcase</a></li> 
     
        <li class='icon'> 
     
        <a href='javascript:void(0);' onclick='myFunction()'>&#9000;</a> 
     
        <!-- responsive icon--> 
     
        </li> 
     
    </ul>

    +0

    谢谢Mark Ni。这也是工作。由于它更紧凑一些,因此喜欢这个片段。 –