js实现淘宝选项卡效果

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			ul{
				overflow: hidden;
				list-style: none;
			}
			li{
				float: left;
				margin-right: 10px;
				border: 1px solid #333;
				cursor: pointer;
			}
			.box{
				width: 300px;
				height: 200px;
				border: 1px solid red;
				display: none;
			}
			.active{
				background-color: #0084BB;
			}
		</style>
		<script type="text/javascript">
			window.onload = function(){
				var aLi = document.getElementsByTagName('li');
				var aBox = document.getElementsByClassName('box');
				// console.log(aLi,aBox);
				for(var i = 0; i < aLi.length; i++){
					aLi[i].index = i;
					aLi[i].onclick = function(){
						for(var i = 0; i < aLi.length; i++){
							aLi[i].className = '';
							aBox[i].style.display = 'none';
						}
						aLi[this.index].className = 'active';
						aBox[this.index].style.display = 'block';
					}
				}
			}
		</script>
	</head>
	<body>
		<ul>
			<li class="active">标题1</li>
			<li>标题2</li>
			<li>标题3</li>
		</ul>
		<div class="box" style="display: block;">标题一内容</div>
		<div class="box">标题二内容</div>
		<div class="box">标题三内容</div>
	</body>
</html>

 

posted @ 2021-07-16 15:56  JackieDYH  阅读(6)  评论(0编辑  收藏  举报  来源