Skip to content

Instantly share code, notes, and snippets.

@Johnqing
Last active December 17, 2015 14:29
Show Gist options
  • Save Johnqing/5625156 to your computer and use it in GitHub Desktop.
Save Johnqing/5625156 to your computer and use it in GitHub Desktop.
html:
<ul>
<li>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
</ul>
script:
var a = document.getElementsByTagName('li');
for(var i = 0;i<a.length;i++){
(function(){
var n = i;
a[i].onclick = function(){
alert(n);
};
})();
}
var a = document.getElementsByTagName('li');
for(var i = 0;i<a.length;i++){
(function(n){
a[i].onclick = function(){
alert(n);
};
})(i);
}
var a = document.getElementsByTagName('li');
for(var i = 0;i<a.length;i++){
a[i].onclick = function(n){
return function(){
alert(n);
}
}(i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment