<body>
<ul id="list">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script>
var list_obj = document.getElementsByTagName('li');
for (var i = 0; i <= list_obj.length; i++) {
(function(){
var p = i
list_obj[i].onclick = function() {
alert(p);
}
})();
}
</script>
</body>
来源: <>