點選的li顯示併為他增加類active,其他的li消失並去除類名,實現選項卡效果
阿新 • • 發佈:2018-11-29
記得引入jquery庫
<!DOCTYPE html> <html> <head> <title>選項卡實現原理</title> <script type="text/javascript" src="js/jquery.1.4.2-min.js"></script> <style type="text/css"> .wrap{width: 300px;list-style: none;} .wrap li{height: 50px;margin:1px;} .active{font-weight:bold; background-color: pink; color:#fff;} </style> </head> <body> <ul class="wrap"> <li class="active">apple</li> <li>pear</li> <li>banana</li> </ul> <script type="text/javascript"> $(function(){ $(".wrap li").click(function() { $(this).siblings('li').removeClass('active'); // 刪除其兄弟元素的樣式 $(this).addClass('active'); // 為點選元素新增類名 }); }); </script> </body> </html>