<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> * {margin: 0;padding: 0;} ul {list-style: none;} .box { 1000px;height: 475px;margin: 100px auto; } .tab { border: 1px solid rgba(205, 211, 211, 0.87); height: 36px; 320px; } .tab-item { 80px;height: 34px;float: left;line-height: 34px; text-align: center;border-top: 3px solid white; /* 3px是为了去掉和hover状态下3pxborder-top的变化过程 */ } span { padding-left: 5px; color: rgba(205, 211, 211, 0.87); } .tab-item:hover {border-top: 3px solid red;} .active {border-top: 3px solid red;} .products {border: 1px solid rgba(205, 211, 211, 0.87);} .products .main {display: none;} .products .selected {display: block;} </style> </head> <body> <div class="box"> <ul class="tab"> <li class="tab-item active">国际大牌<span>|</span></li> <li class="tab-item">国妆名牌<span>|</span></li> <li class="tab-item">清洁用品<span>|</span></li> <li class="tab-item">男士精品</li> </ul> <div class="products"> <div class="main selected"> <a href="###"><img src="images/guojidapai.jpg" alt=""/></a> </div> <div class="main"> <a href="###"><img src="images/guozhuangmingpin.jpg" alt=""/></a> </div> <div class="main"> <a href="###"><img src="images/qingjieyongpin.jpg" alt=""/></a> </div> <div class="main"> <a href="###"><img src="images/nanshijingpin.jpg" alt=""/></a> </div> </div> </div> <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script> <script> $(function(){ $("li").mouseenter(function(){ $(this).addClass("active"); $(this).siblings().removeClass("active"); // 给li注册事件,this添加active同时去掉其他相邻兄弟的active var idx=$(this).index();//当前的索引放在idx中 $(".main").eq(idx).addClass("selected").siblings().removeClass("selected"); //.main中当前索引添加selected,其他去掉selected }); }); </script> </body> </html>