<ul id="nav">
<li><a href="/about/">About Us</a></li>
<li><a href="/contact/">Contact Us</a></li>
<li><a href="/archives/">Our Archives</a></li>
<li><a href="/free/">Free Stuff</a></li>
</ul>js代码:
function setActive() {
aObj = document.getElementById('nav').getElementsByTagName('a');
for(i=0;i<aObj.length;i++) {
if(document.location.href.indexOf(aObj[i].href)>=0) {
aObj[i].className='active';
}
}
}我不太明白。1,first aObj is get the all a label in nav
2,aObj.length等于4,
3,document.location.href.indexOf(aObj[i].href这行是什么意思?
4,为什么循环可以将active添加到当前的a中?
发布于 2011-05-28 15:59:07
3,document.location.href.indexOf(aObji.href这行是什么意思?
if(document.location.href.indexOf(aObj[i].href)>=0)检查<a>标签的href是否是当前页面的href的一部分。如果indexOf方法返回的值大于或等于0,那么我们就知道这是当前页面,并且需要是活动类。
4,为什么循环可以将活动添加到当前的a?
aObj[i].className='active';<a>标记具有ClassName属性,可以使用javascript检索和设置该属性。它正在将active类设置为当前页面链接。
https://stackoverflow.com/questions/6160414
复制相似问题