如圖所示 代碼如下圖:
特別要注意的是:a標簽不會繼承上級的color,所以要單獨為其設置
參看代碼(並非上圖代碼)如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>點擊切換Css類</title> <style> .nav{ width: 690px; height: 80px; margin: 0 auto; border: 2px solid #0797B5; } .nav ul li { display: inline; list-style: none; } .nav ul li{ border: 1px solid solid; margin: 14px; text-decoration: none; font-size: 20px; padding-bottom: 10px; } .navClicked{ font-weight: bold; color: #c61919; border-bottom: 3px solid #c61919; cursor: pointer; } .nav ul li:hover { font-weight: bold; color: #c61919; cursor: pointer; } </style> <script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script> <script> // 導航切換 $(function(){ //第一種方法 $(".nav ul li").each(function(index) { $(this).click(function() { $(".nav ul li").eq(index).addClass("navClicked").siblings().removeClass("navClicked"); }) }) //第二種方法 /*$(".nav ul li").click(function(){ $(this).addClass("navClicked").siblings().removeClass("navClicked"); });*/ }); </script> </head> <body> <div class="nav"> <ul> <li class="navClicked">首頁</li> <li>美國留學</li> <li>留學咨訊</li> <li>院校庫</li> <li>合作伙伴</li> <li>關於我們</li> </ul> </div> </body> </html>
效果圖如下:
轉自博客:https://blog.csdn.net/qq_39588630/article/details/82800343