I have a list of 3 links, that I wanna iterate through, so each one of them can do something.
But when I use the for-loop, it only gives me 3
in the console, which is the number of links in the list.
I wanna the console to show each of them like so: 0, 1, 2
;
另外,我如何获得每个链接的索引位置?
见以下代码:http://jsfiddle.net/c8Wdj/
请不要 JQuery 或任何库...
JavaScript:
(function(){
var triggers = document.getElementById( some-list ).getElementsByTagName( a );
for (var i = 0, max = triggers.length; i < max; i += 1) {
triggers[i].addEventListener( mouseenter , function(e) {
console.log(i);
}, false);
}
}());
HTML:
<ul id="some-list">
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
<li><a href="#">Three</a></li>
</ul>调
调