Search

1/14/2009

closure

Looks Good Works Well


function main(links) {
for (var i = 0; i < links.length; i++) {
links[i].onclick = function() {
alert(i + 1);
}
}
};
main(document.getElementsByTagName("a"));


function main(links) {
for (var i = 0; i < links.length; i++) {
links[i].onclick = (function(i) {
return function() {
alert(i + 1);
}
})(i);
}
};
main(document.getElementsByTagName("a"));

沒有留言: