Possible Duplicate:
How do JavaScript closures work?
<script type="text/javascript">
function init() {
var pArry = document.getElementsByTagName("p");
for( var i=0; i<pAry.length; i++ ) {
pArry[i].onclick = function() {
alert(i);
}
}
}
</script>
</head>
<body onload="init();">
<p>test 0</p>
<p>test 1</p>
<p>test2</p>
<p>test3</p>
<p>test4</p>
why the results are all 5? i want the reault is (0,1,2....).