先放效果图吧
![4830c6f62c03b6e40e571682ca853307.png](https://i-blog.csdnimg.cn/blog_migrate/4978d120bf0e19d72d690dc45718ecce.png)
第一种 html+css +jquery 记得引入juqery文件 我偏向于第一种,eq是遍历
jQuery ±éÀú - eq() ·½·¨www.w3school.com.cn这是他的官方解释,点击链接即可
![307def2874e04c7314d734a46313a481.png](https://i-blog.csdnimg.cn/blog_migrate/41c767f8cb99abeddd829c7879dd00d5.png)
上图是部分代码图 样式自己设置即可
完整demo
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.title {
width: 200px;
display: flex;
}
.title div {
height: 30px;
font-size: 16px;
flex: 1;
border: 1px solid;
text-align: center;
line-height: 30px;
}
.active {
color: red;
}
.content {
width: 200px;
border: 1px solid;
}
.content div {
width: 100%;
height: 200px;
font-size: 18px;
display: none;
}
.content div:nth-child(1) {
background-color: green;
display: block;
}
.content div:nth-child(2) {
background-color: yellow;
}
.content div:nth-child(3) {
background-color: goldenrod;
}
</style>
</head>
<body>
<div class="title">
<div class="active">语文</div>
<div>数学</div>
<div>英语</div>
</div>
<div class="content">
<div>语文</div>
<div>数学</div>
<div>英语</div>
</div>
</body>
<script src='./js/jquery.js'></script>
<script>
$(function() {
$(".title div").click(function() {
$('.title div').removeClass('active')
$(this).addClass('active')
$('.content div').hide()
$('.content div').eq($('.title div').index(this)).show()
})
})
</script>
</html>
//02 html+css+javascript效果图和juqery一样我直接贴代码图吧
![a3609a072afb32a4715c4618b393ff89.png](https://i-blog.csdnimg.cn/blog_migrate/7dc9ca5d8b52e0319d5b70e5be6a1264.jpeg)
样式可以写哦,
下面我放一下完整demo 用的是循环遍历的写法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
li {
list-style: none;
}
.title {
width: 200px;
display: flex;
}
.title li {
height: 30px;
font-size: 16px;
flex: 1;
border: 1px solid;
text-align: center;
line-height: 30px;
}
.act {
color: red;
}
.content {
width: 200px;
border: 1px solid;
}
.content div {
width: 100%;
height: 200px;
font-size: 18px;
display: none;
}
.content div:nth-child(1) {
background-color: green;
display: block;
}
.content div:nth-child(2) {
background-color: yellow;
}
.content div:nth-child(3) {
background-color: goldenrod;
}
</style>
</head>
<body>
<nav class="title">
<li class="act">语文</li>
<li>数学</li>
<li>英语</li>
</nav>
<div class="content">
<div class="tab">语文</div>
<div class="tab">数学</div>
<div class="tab">英语</div>
</div>
</body>
<script>
window.onload = function() {
var title = document.getElementsByTagName('li')
var cont = document.getElementsByClassName('tab')
for (var i = 0; i < title.length; i++) {
title[i].index = i;
title[i].onclick = function() {
for (var i = 0; i < title.length; i++) {
title[i].className = '';
cont[i].style.display = 'none'
}
this.className = 'act';
cont[this.index].style.display = 'block'
}
for (var m = 1; m < title.length; m++) {
title[m].className = '';
cont[m].style.display = 'none'
}
}
}
</script>
</html>