js选项卡代码详解

**

js选项卡

**

	<body>
		<input type="button" name="" id="btn1" value="选项一" style="background-color: yellow;"/>
		<input type="button" name="" id="btn2" value="选项二"/>
		<input type="button" name="" id="btn3" value="选项三"/>
		<div style="display:block;">1111</div>
		<div>2222</div>
		<div>3333</div>
		
	<script>
		var input1=document.querySelectorAll('input');
		var div1=document.querySelectorAll('div');
		var last=input1[0];//这个变量存的是上一次的对象        

		for(var i=0;i<input1.length;i++){
			
			input1[i].index=i;
			//自定义属性
				
			input1[i].onclick=function(){
				//把上一次点击对象的背景色去掉
				last.style.background='';
				
				//把上一个对应的div,让它隐藏
				div1[last.index].style.display='none';
				
				//给当前点击的那个按钮添加背景色
				this.style.background='yellow';
				
				//让当前点击的按钮对应的div,显示
				div1[this.index].style.display='block';
				
				last=this;//把上一次点击的对象更新成当前点击的对象
			}
		}

猜你喜欢

转载自blog.csdn.net/qq_34146679/article/details/85318809