<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>选项卡</title>
</head>
<style>
*{
margin: 0px;padding:0px;box-sizing: border-box;
}
html,body{
width: 100%;height: 100%;
}
.tab{
width: 40rem;
height: 30rem;
border: solid 1px#000000;
}
.tab-title{
width: 100%;
height: 5rem;
background-color:bisque;
border-bottom: solid 2px orange;
display:flex;
align-items: center;
justify-content: space-between;
}
.tt{
height: 5rem;
flex:1;
background-color:#fff;
font-size: 1.8rem;
text-align: center;
line-height: 5rem;
cursor:pointer;
}
.tt.active,
.tt:hover{
background-color: orange;
color:white;
}
.tab-content{
width: 100%;
height: calc(100% - 5rem);
background-color: aqua;
position:relative;
}
.tc{
width: 100%;
height: 100%;
font-size:1.8rem;
text-align: center;
color:white;
display:none;
position: absolute;
}
.tc.active{
display:block;
}
.tc:nth-of-type(1){
background-color:blue;
}
.tc:nth-of-type(2){
background-color: green;
}
.tc:nth-of-type(3){
background-color: red;
}
</style>
<body>
<div class="tab">
<div class="tab-title">
<div class="tt active">标题1</div>
<div class="tt">标题2</div>
<div class="tt">标题3</div>
</div>
<div class="tab-content">
<div class="tc active">内容1</div>
<div class="tc">内容2</div>
<div class="tc">内容3</div>
</div>
</div>
<script>
let tts = document.getElementsByClassName("tt")
for(let i=0;i<tts.length;i++){
console.log(tts[i])
tts[i].onmouseenter = function(){
for(let j=0;j<tts.length;j++){
tts[j].classList="tt"
}
tts[i].classList="tt active"
let tcs=document.getElementsByClassName("tc")
for(x=0;x<tcs.length;x++){
tcs[x].classList = "tc"
}
tcs[i].classList = "tc active"
}
}
</script>
</body>
</html>





被折叠的 条评论
为什么被折叠?



