-
-
Save eiel/1127597 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var links = document.getElementsByTagName("a"); | |
for ( var i = 0 ; i < links.length ; i++ ){ | |
(function() { | |
var n = i; | |
var link = links[i]; | |
link.href = "#"; | |
link.onclick = function(){ alert( n ) }; | |
link.innerHTML = n; | |
})() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
alert(i)したときに はじめて i の値をアクセスしにいき、どのリンクも同じ i を読んでしまう。
そこで、ループごとに変数を作成してaleartするときの 実体が違う n をよみにいくようにする。
aの数だけ新しい nを作っているという感じ。