<!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>
<!-- 引入jQuery -->
<script src="./js/jquery.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
.container {
width: 800px;
margin: 0 auto;
}
ul {
width: 800px;
margin: 0 auto;
display: flex;
justify-content: space-between;
}
ul li {
list-style-type: none;
width: 200px;
height: 50px;
background-color: lightcyan;
line-height: 50px;
text-align: center;
font-size: 25px;
}
.container .content > div {
width: 800px;
margin: 0 auto;
background-color: lightgrey;
position: absolute;
display: none;
}
.container .content div:nth-child(1) {
display: block;
}
.on {
background-color: lightpink;
}
</style>
</head>
<body>
<div class="container">
<ul class="header">
<li class="on">首页</li>
<li>郑州</li>
<li>国内</li>
<li>国际</li>
</ul>
<div class="content">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
</div>
</body>
<script>
$(function () {
$(".container .header li").bind("mouseover", function () {
$(this).addClass("on").siblings().removeClass();
var $index = $(this).index();
$(".content div").eq($index).show(); //当前li的索引对应的div显示
$(".content div").eq($index).show().siblings().hide(); //当前li的其他兄弟隐藏
});
});
</script>
</html>
jQuery实现选项卡的功能
最新推荐文章于 2024-08-26 12:07:23 发布