<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
ul,
ol {
list-style: none;
}
.header {
width: 600px;
height: 50px;
line-height: 50px;
text-align: center;
background-color: pink;
display: flex;
}
.header li {
width: 200px;
}
.content {
width: 600px;
height: 400px;
line-height: 400px;
text-align: center;
}
.header .active {
background-color: aquamarine;
}
.content li {
display: none;
font-size: 50px;
}
.content .active {
display: block;
background-color: orange;
}
</style>
</head>
<body>
<ul class="header">
<li>header_1</li>
<li class="active">header_2</li>
<li>header_3</li>
</ul>
<ol class="content">
<li>content_1</li>
<li class="active">content_2</li>
<li>content_3</li>
</ol>
<script>
const oHeader = document.querySelectorAll('.header>li');
const oContent = [...document.querySelectorAll('.content>li')];
oHeader.forEach(function (value1 , index1) {
console.log(value1);
value1.onclick = function () {
oHeader.forEach(function (value2 , index2) {
value2.classList.remove('active');
oContent[index2].classList.remove('active');
})
value1.classList.add('active');
oContent[index1].classList.add('active');
}
})
</script>
</body>
</html>