<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.select {
width: 300px;
display: flex;
justify-content: space-evenly;
}
.select button {
width: 80px;
height: 30px;
background-color: antiquewhite;
text-align: center;
border: 2px solid black;
border-radius: 10px;
}
.select button:active {
background-color: aqua;
}
.box {
width: 300px;
height: 200px;
background-color: aliceblue;
}
.box>.smallbox {
width: 300px;
height: 200px;
display: none;
}
</style>
<body>
<div class="select" id="btnGroup">
<button>选项1</button>
<button>选项2</button>
<button>选项3</button>
</div>
<div class="box">
<div class="smallbox" style="background-color: red;display: block;" >1</div>
<div class="smallbox" style="background-color: blue;">2</div>
<div class="smallbox" style="background-color: green;">3</div>
</div>
<script>
let btn=document.getElementsByTagName('button')
let smallbox=document.getElementsByClassName('smallbox')
console.log(smallbox);
for (let i = 0; i < 3; i++) {
btn[i].addEventListener('click',function(){
console.log(i);
smallbox[i].style.display='block'
for(let j = 0; j < 3;j++){
if(j!==i){
smallbox[j].style.display='none'
}
}
})
}
</script>
</body>
</html>