<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul>
<li>菜单一</li>
<li>菜单一</li>
<li>菜单一</li>
<li>菜单一</li>
</ul>
<script>
var lis = document.getElementsByTagName("li");
for(var i=0;i<lis.length;i++){
var li = lis[i];
/*
li.addEventListener('click',function(e){
alert(i);
})
*/
li.addEventListener('click',(function(num){
return function(e){
alert(num);//if(num==1)
}
})(i))
};
/*
var fn = (function(num){
return function(e){alert(num)}
})(i)
li.addEventListener('click',fn);
*/
/*解决一下:怎么实现点击对应的菜单出现的是对应的0、1、2、3弹框*/
/*
debug思路:
1.定位问题
2.原因
3.解决问题 (function(){})()
var f=(function(i){return funciton(e){alert(i)}})(i)
4.分析问题
*/
</script>
</body>
</html>
js--闭包--菜单例子
最新推荐文章于 2024-08-22 14:32:34 发布