美文网首页
JS 当前栏目高亮显示

JS 当前栏目高亮显示

作者: cuihaomaster | 来源:发表于2018-06-27 13:11 被阅读0次

    html结构:

    <div id="nav">
        <ul>
            <li><a href="#1">首页</a></li>
            <li><a href="#2">产品</a></li>
            <li><a href="#3">地址</a></li>
            <li><a href="#4">关于</a></li>
            <li><a href="#5">联系</a></li>
        </ul>
    </div>
    

    css 样式:

    body{font-family: "微软雅黑";}
    .active{
        background: -webkit-linear-gradient(top,red,yellow);
    }
    ul{float: left;width: 500px;height: 30px;background: red;line-height: 30px;}
    li{list-style: none;float: left;}
    a{text-align: center;line-height: 30px;display: block;color: #fff;width: 70px;text-decoration: none;}
    a:hover{background: green;}
    

    JS:

    <script type="text/javascript">
            var myNav=document.getElementById("nav").getElementsByTagName("a");
            var currenturl = document.location.href;
            for(var i=0;i<myNav.length;i++){
                var aurl=myNav[i].getAttribute("href");
                if(currenturl.indexOf(aurl)!=-1){
                    myNav[i].className="active";
                    myNav[0].className="";
                }
            }
     </script>
    

    相关文章

      网友评论

          本文标题:JS 当前栏目高亮显示

          本文链接:https://www.haomeiwen.com/subject/xqubyftx.html