导航栏高亮实例
html代码
<div id="navi">
<ul>
<li><a href="">主页1</a><li>
<li><a href="">主页2</a><li>
<li><a href="">主页3</a><li>
<li><a href="">主页4</a><li>
</ul>
</div>
css代码部分
给li添加类名hl
#nav ul li.hl a {
color: red;
}
js代码部分
var nav = document.getElementById("navi");
var links = nav.getElementsByTagName("li");
var lilen = nav.getElementsByTagName("a");
var currenturl = document.location.href;
var last = 0;
for (var i=0;i<links.length;i++)
{
var linkurl = lilen.getAttribute("href");
if(currenturl.indexOf(linkurl)!=-1)
{
last = i;
}
}
links[last].className = "current";