闭包(闭包/闭包循环的索引值/ 闭包做私有变量计时器/闭包做选项卡)

闭包

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>闭包</title>
	<script type="text/javascript">
		/*
		闭包的本质就是函数嵌套,就是在函数里面定义函数,
		内部函数可以引用外部函数的参数和变量
		参数和变量不会被垃圾回收机制给回收
		闭包的用途:可以存循环的索引值、做私有变量计数器
		*/
		/*
		//闭包的一般写法
		function aa(b){
			var a = 12;

			function bb(){
				alert(a);
				alert(b);
			}

			return bb;
		}

		var cc = aa(24);*/

		
		//闭包的封闭函数写法
		var cc = (function(b){
			var a = 12;

			function bb(){
				alert(a);
				alert(b);
			}

			return bb;
		})(24);

		cc();
		

		/*
		//只能调用一次的闭包
		(function(b){
			var a = 12;

			function bb(){
				alert(a);
				alert(b);
			}

			return bb;
		})(24)();
		*/
	</script>
</head>
<body>
	
</body>
</html>

闭包循环的索引值

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>闭包存循环的索引值</title>
	<style type="text/css">
		li{
			height: 30px;
			background-color: gold;
			margin-bottom: 10px;
		}
	</style>
	<script type="text/javascript">
		//闭包的用途:存循环的索引值
		window.onload = function(){
			var aLi = document.getElementsByTagName('li');
			// alert(aLi.length);//8

			for(var i=0; i<aLi.length; i++){
				/*
				aLi[i].onclick = function(){
					alert(i);//每个li都弹出8,因为点击时循环已完毕,i最后为8
				}
				*/

				(function(k){//这里的k是形参
					aLi[k].onclick = function(){
						alert(k);//弹出每个li的索引值
					}
				})(i);//这里的i是实参
			}
		}
	</script>
</head>
<body>
	<ul>
		<li>1</li>
		<li>2</li>
		<li>3</li>
		<li>4</li>
		<li>5</li>
		<li>6</li>
		<li>7</li>
		<li>8</li>
	</ul>
</body>
</html>

闭包做私有变量计时器

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>闭包做私有变量计数器</title>
	<script type="text/javascript">
		//闭包的用途:私有变量计数器
		var count = (function(){
			var a = 0;

			function bb(){
				a++;
				return a;
			}

			return bb;
		})();
		
		//每调用一次count,a就自增一次
		alert(count());//1
		alert(count());//2

		var c = count();
		alert(c);//3
	</script>
</head>
<body>
	
</body>
</html>

闭包做选项卡

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>闭包做选项卡</title>
	<style type="text/css">
		.btns{
			width: 500px;
			height: 50px;
		}
		/*选项卡的样式*/
		.btns input{
			width: 100px;
			height: 50px;
			background-color: #ddd;/*默认灰色*/
			color: #666;
			border: 0px;
		}
		/*被选中的选项卡的样式*/
		.btns input.cur{
			background-color: gold;
		}
		/*内容区的样式*/
		.contents div{
			width: 500px;
			height: 300px;
			background-color: gold;
			display: none;/*默认隐藏*/
			line-height: 300px;
			text-align: center;
		}
		/*被选中的内容区的样式*/
		.contents div.active{
			display: block;
		}
	</style>
	<script type="text/javascript">
		//闭包做选项卡
		window.onload = function(){
			var aBtn = document.getElementById('btns').getElementsByTagName('input');
			var aCon = document.getElementById('contents').getElementsByTagName('div');
			// alert(aCon.length);

			//循环所有的选项卡按钮
			for(var i=0; i<aBtn.length; i++){
				(function(i){
					//给每个选项卡按钮添加点击事件
					aBtn[i].onclick = function(){
						//遍历所有选项卡按钮
						for(var j=0; j<aBtn.length; j++){
							//将每个选项卡按钮都设为灰色
							aBtn[j].className = '';
							//将每个内容区都隐藏
							aCon[j].className = '';
						}
						//this代表当前点击的Button对象
						this.className = 'cur';//当前点击的按钮为金色

						// alert(i);//不加闭包时,不管点哪个按钮,i都等于3

						//加闭包保存了索引值才有效
						aCon[i].className = 'active';//当前点击的按钮对应的内容显示
					}
				})(i);
			}
		}
	</script>
</head>
<body>
	<div class="btns" id="btns">
		<input type="button" value="tab01" class="cur">
		<input type="button" value="tab02">
		<input type="button" value="tab03">
	</div>
	<div class="contents" id="contents">
		<div class="active">tab文字内容一</div>
		<div>tab文字内容二</div>
		<div>tab文字内容三</div>
	</div>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_43152725/article/details/86172354