zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Google Chart API 阮一峰的网络日志
    PHP随机函数【上】
    php实现socket推送技术
    javascript变量作用域
    如何使用jqplot描绘一个简单的线形图?
    培训小记
    Google自己的浏览器GoogleChrome
    这大半年的回顾
    一个高手的SQL求工作天数的函数
    关于TSQL中数据库重命名
  • 原文地址:https://www.cnblogs.com/zxf100/p/11569388.html
Copyright © 2011-2022 走看看