HTML+CSS+JS做简易三色卡

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<style type="text/css">
		*{
			margin: 0;
			padding: 0;
		}
		.bigbox{
			width: 600px;
			height: 500px;
			border: 1px solid #008000;
			margin: 0 auto;
		}
		.bigbox>button{
			width: 200px;
			height: 50px;
			float: left;
		}
		.bigbox>.box{
			width: 600px;
			height: 450px;
			margin-top: 50px;
			position: absolute;
		}
		.box:nth-child(4){
			background-color: red;
		}
		.box:nth-child(5){
			background-color: blue;
		}
		.box:nth-child(6){
			background-color: green;
		}
	</style>
	<body>
		<div class="bigbox">
			<button type="button">红色</button>
			<button type="button">蓝色</button>
			<button type="button">绿色</button>
			<div class="box"></div>
			<div class="box"></div>
			<div class="box"></div>
		</div>
	</body>
	<script type="text/javascript">
		var obtn=document.querySelectorAll("button")
		var obox=document.querySelectorAll(".box")
		for(a=0;a<obtn.length;a++){
			//把a这个下标存储到index中
			obtn[a].index=a;
			//给所有的按钮绑定一个点击事件
			obtn[a].onclick=function(){
				//循环所有的按钮进行隐藏
				for(c=0;c<obox.length;c++){
					obox[c].style.display="none"
				}
				//跟按钮下标对应的div显示
				obox[this.index].style.display="block"
			}
		}
	</script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值