Skip to content

Instantly share code, notes, and snippets.

@itboos
Created September 12, 2016 09:28
Show Gist options
  • Save itboos/2c0cf87b8c342bddd7889b8beeaf4417 to your computer and use it in GitHub Desktop.
Save itboos/2c0cf87b8c342bddd7889b8beeaf4417 to your computer and use it in GitHub Desktop.
点击Li实现循环弹出自己的索引
<!doctype html>
<html lang="en" ng-app="myTest">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>今天是9月12号,a new day!</title>
<style type="text/css">
#ul1 li{
list-style-type: none;
width:200px;
height:30px;
background:#ccc;
margin-bottom: 10px;
cursor: pointer;
}
</style>
<script type="text/javascript">
window.onload=function(){
var aLis=document.getElementById("ul1").getElementsByTagName("li");
console.log(aLis);
for(var i=0;i<aLis.length;i++){
(function(n){
aLis[i].onclick=function(){
alert(n);
}
})(i);
}
}
</script>
</head>
<body>
<ul id="ul1">
<li>这是第1个LI</li>
<li>这是第2个LI</li>
<li>这是第3个LI</li>
<li>这是第4个LI</li>
<li>这是第5个LI</li>
</ul>
</body>
</html>
@itboos
Copy link
Author

itboos commented Sep 12, 2016

闭包二三事配例子

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment