<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<a href="#">1</a>
<a href="#">2</a>
<a href="#">3</a>
<a href="#">4</a>
<script type="text/javascript">
var ali = document.getElementsByTagName('a');
for(var i=0;i<ali.length;i++){
(function(){
var p = i ;
console.log(p);
ali[i].onclick = function() {
alert(p+1);
}
})();
}
</script>
</body>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<a href="#">1</a>
<a href="#">2</a>
<a href="#">3</a>
<a href="#">4</a>
<script type="text/javascript">
var ali = document.getElementsByTagName('a');
for(var i=0;i<ali.length;i++){
(function(){
var p = i ;
console.log(p);
ali[i].onclick = function() {
alert(p+1);
}
})();
}
</script>
</body>
</html>
ali[i].onclick = (function(m) {
return function(){
alert(m+1);
};
})(i);