
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tab-jQuery</title>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
.container {
width: 600px;
margin: 100px auto 0;
overflow: hidden;
}
.tab_list {
height: 30px;
width: 600px;
}
.tab_list li {
float: left;
height: 30px;
line-height: 30px;
width: 200px;
font-size: 20px;
text-align: center;
color: #ccc;
background: green;
cursor: pointer;
}
.tab_con li {
display: none;
height: 200px;
width: 600px;
background: pink;
font-size: 100px;
line-height: 200px;
text-align: center;
}
.tab_list .current {
background: red;
color: #ccc;
}
.tab_con .item {
display: block;
}
</style>
</head>
<body>
<div class="container">
<ul class="tab_list">
<li class="current">标题1</li>
<li>标题2</li>
<li>标题3</li>
</ul>
<ul class="tab_con">
<li class="item">内容111</li>
<li>内容222</li>
<li>内容333</li>
</ul>
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$('.tab_list li').click(function () {
var index = $(this).index();
$(this).addClass('current').siblings().removeClass('current');
$('.tab_con li').eq(index).show().siblings().hide();
});
</script>
</html>

原理
- 点击上部的 li,当前 li 添加 current 类,其余兄弟移除类
- 点击的同时,得到当前 li 的索引号
- 让下部里面相应索引号的 item 显示,其余的 item 隐藏
来源:https://www.xwsir.cn/2775.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构