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

[(屏幕截图 2025-03-31 153850.png-aa740d-1743406736979-0)]

屏幕截图 2025-03-31 153850.png
下拉菜单
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,body{
width: 100%;
height: 100%;
}
html{
font-size: 20px;
}
.nav{
width: 100%;
height: 6rem;
background-color: bisque;
display: flex;
justify-content: center;
}
.content{
width: 40rem;
height: 5rem;
background-color: bisque;
display: flex;
justify-content: start;
padding: 5px;
}
.content .item{
width: 10rem;
height: 5rem;
background-color: bisque;
padding: 0 1rem;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.content .item:hover{
background-color: rgb(6, 1, 10);
color: yellow;
cursor: pointer;
}
.content .item ul{
position: absolute;
top: 100%;
left: 0;
height: 50px;
width: 10rem;
background-color: rgb(146, 14, 223);
display: none;
flex-direction: column;
justify-content: center;
align-items: center;
}
.fu{
background-color: rgb(146, 14, 223);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div class="nav">
<ul class="content">
<li class="item">首页</li>
<li class="item">文章
<ul>
<li class="fu">我的文章</li>
<li class="fu">发表文章</li>
</ul>
</li>
<li class="item">相册
<ul>
<li class="fu">我的相册</li>
<li class="fu">发表相册</li>
</ul>
</li>
<li class="item">消息
<ul>
<li class="fu">我的私信</li>
<li class="fu">发表消息</li>
</ul>
</li>
</ul>
</div>
<script>
let menu1 = document.getElementsByClassName("item");
for (let i = 0; i < menu1.length; i++) {
menu1[i].onmouseenter = function () {
let subMenu = this.getElementsByTagName("ul")[0];
if (subMenu) {
subMenu.style.display = "flex";
}
};
menu1[i].onmouseleave = function () {
let subMenu = this.getElementsByTagName("ul")[0];
if (subMenu) {
subMenu.style.display = "none";
}
};
}
</script>
</body>
</html>

屏幕截图 2025-03-31 154214.png