码迷,mamicode.com
首页 > 其他好文 > 详细

用闭包的写法弹出元素的索引值

时间:2016-10-24 09:40:45      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:.com   text   cli   document   his   http   har   alert   eve   

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="">
<meta name="keywords" content="">
<title>用闭包的写法弹出元素的索引值</title>
<!-- <script type="text/javascript" src="jquery-1.10.1.min.js"></script> -->
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
</head>
<script type="text/javascript">
// jquery写法;
// $(function() {
// $(‘li‘).click(function(event) {
// alert($(this).index());
// });
// });


// js原生写法一;
// window.onload=function(){
// var aLi=document.getElementsByTagName(‘li‘);
// for (var i=0;i<aLi.length;i++) {
// var aa=function(i){
// aLi[i].onclick=function(){
// // console.log(i);
// alert(i);
// };
// };
// aa(i);
// }
// }

// js原生写法二闭包;
window.onload=function(){
var aLi=document.getElementsByTagName(‘li‘);
for (var i=0;i<aLi.length;i++) {
(function(i){
aLi[i].onclick=function(){
// console.log(i);
alert(i);

};
})(i);
}
}

</script>
<body>
<ul>
<li>1111</li>
<li>1111</li>
<li>1111</li>
</ul>
</body>
</html>

用闭包的写法弹出元素的索引值

标签:.com   text   cli   document   his   http   har   alert   eve   

原文地址:http://www.cnblogs.com/ll-taj/p/5991750.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!