點擊導航菜單,切換選中狀態 效果: 思路:首先獲取選中的URL,再通過正則判斷是否相同,相同就加上相應的屬性,不相同就去除相應的屬性。 html代碼 JQ代碼 ...
點擊導航菜單,切換選中狀態
效果:
思路:首先獲取選中的URL,再通過正則判斷是否相同,相同就加上相應的屬性,不相同就去除相應的屬性。
html代碼
<div class="layui-container" style="padding: 0px; margin-top: 70px;"> <div class="layui-row footer-nav"> <div class="layui-col-xs2 layui-col-sm2 item"> <a href="{:url('/index/index/index')}"> <img src="/public/index/images/icon/home.png"> <div >首頁</div> </a> </div> <div class="layui-col-xs2 layui-col-sm2 item"> <a href="{:url('/index/index/videos')}"> <img src="/public/index/images/icon/video.png"> <div>視頻</div> </a> </div> <div class="layui-col-xs2 layui-col-sm2 item"> <a href="{:url('/index/index/music')}"> <img src="/public/index/images/icon/music.png"> <div>音樂</div> </a> </div> <div class="layui-col-xs2 layui-col-sm2 item"> <a href="{:url('/index/index/news')}"> <img src="/public/index/images/icon/new.png"> <div>動態</div> </a> </div> <div class="layui-col-xs2 layui-col-sm2 item"> <a href="{:url('/index/index/about')}"> <img src="/public/index/images/icon/about.png"> <div>關於</div> </a> </div> </div> </div> <style type="text/css"> .active{ color: red } </style>
JQ代碼
<script type="text/javascript"> var url = location.href; $('.item > a').each(function () { pat = new RegExp('/index/index/index'); if (pat.test(url)){ $('.item>a').eq(0).addClass('active'); $('.item>a>img').eq(0).attr("src","/public/index/images/icon/home_selected.png"); } pat = new RegExp('/index/index/videos'); if (pat.test(url)){ $('.item>a').eq(0).removeClass('active'); $('.item>a').eq(1).addClass('active'); $('.item>a>img').eq(1).attr("src","/public/index/images/icon/video_selected.png"); } pat = new RegExp('/index/index/music'); if (pat.test(url)){ $('.item>a').eq(1).removeClass('active'); $('.item>a').eq(2).addClass('active'); $('.item>a>img').eq(2).attr("src","/public/index/images/icon/music_selected.png"); } pat = new RegExp('/index/index/news'); if (pat.test(url)){ $('.item>a').eq(2).removeClass('active'); $('.item>a').eq(3).addClass('active'); $('.item>a>img').eq(3).attr("src","/public/index/images/icon/new_selected.png"); } pat = new RegExp('/index/index/about'); if (pat.test(url)){ $('.item>a').eq(3).removeClass('active'); $('.item>a').eq(4).addClass('active'); $('.item>a>img').eq(4).attr("src","/public/index/images/icon/about_selected.png"); } }); </script>