代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>选项卡</title>
<style>
body{
background: #000;
}
div{
width: 300px;
height: 300px;
border: 1px solid #000;
display: none;
border-radius: 30px;
margin-top: 10px;
color: #fff;
text-align: center;
line-height: 300px;
font-size: 100px;
}
input{
width: 100px;
height: 40px;
display: inline-block;
cursor: pointer;
}
</style>
</head>
<body>
<section id="box">
<input type="button" value="按钮1" style="background: hotpink">
<input type="button" value="按钮2">
<input type="button" value="按钮3">
<div style="display: block;border-color: hotpink">1</div>
<div>2</div>
<div>3</div>
</section>
</body>
<script>
window.onload=function () {
var btns=document.getElementsByTagName('input');
var oDiv=document.getElementsByTagName('div');
for(var i=0;i<btns.length;i++){
btns[i].index=i;
btns[i].onclick=function () {
for(var i=0;i<oDiv.length;i++){
btns[i].style.background='';
oDiv[i].style.display='none';
oDiv[i].style.borderColor='hotpink';
}
oDiv[this.index].style.display='block';
oDiv[this.index].style.borderColor='hotpink';
this.style.background='hotpink';
}
}
}
</script>
</html>
