为菜单2级别添加活动,hiliting 2级菜单jquery

时间:2015-05-31 13:22:39

标签: jquery html css html5 css3

我有一个2级菜单,当我点击第一级时,它突出显示黄色并且工作正常。

但是,当我从第二级选择一个类别时,我希望第一级的主要类别也能突出显示,以便用户知道他正在浏览哪一个主要类别。

这是我的网站: http://www.marseille-fitness.com/bodypump.php

<ul class="mainNav">
    <li><a href="rpm.php">Cours collectifs</a>
        <ul class="dropDown">
            <li><a href="rpm.php">RPM</a></li>
            <li><a href="bodypump.php">Bodypump</a></li>
            <li><a href="bodyattack.php">Bodyattack</a></li>
            <li><a href="bodycombat.php">Bodycombat</a></li>
            <li><a href="bodyJam.php">Bodyjam</a></li>
            <li><a href="bodybalance.php">Bodybalance</a></li>
            <li><a href="cxworx.php">Cxworx</a></li>
        </ul>
    </li>
</ul>

<script>



        $("ul.mainNav li").hover(function(){
        $(this).find("ul.dropDown").css({"display" : "block"}).fadeTo('500', '1.0');
    }, function() {
        $(this).find("ul.dropDown").fadeTo('500', '0.0').css({"display" : "none"});
    });
        //class active in menu start
    $(function(){

        var url = window.location.pathname;
        var urlRegExp = new RegExp(url.replace(/\/$/,'')); // create regexp to match current url pathname and remove trailing slash if present as it could collide with the link in navigation in case trailing slash wasn't present there

        // now grab every link from the navigation
        $('.mainNav li a').each(function(){
            // and test its href against the url pathname regexp
            if(urlRegExp.test(this.href)){
                $(this).addClass('active');

            }
        });

    });

</script>

CSS

    @import url(http://fonts.googleapis.com/css?family=Raleway:600,800);

ul.mainNav {
    margin:0;
    padding:0;
    list-style-type:none;
    background-color: #f0eff0;
    height: 28px;
    font-family: 'Raleway', sans-serif;
    color: #606060;
    float: right;
    margin-right: 100px;

}
.menu-wrapper{
    background-color: #f0eff0;
    height: 3.4em;
}


.mainNav li {
    margin:0;
    padding:0;
    list-style-type:none;
    height: 28px;
    line-height:28px;
    float: left;
    color: #606060;
    font-size: 14px;
}
.mainNav li a {
    padding: 0 20px;
    line-height: 3.8em;
    display:block;
    color: #606060;
    text-decoration:none;
    font-family: 'Raleway', sans-serif;
    font-weight: 800;
}

.mainNav li:hover > a {
    background: #ffffff;
}

.mainNav li a:hover{
    background: #ffffff;
}

.mainNav li ul.dropDown {
    margin:0;
    padding:0;
    position: absolute;
    display: none;
    background: #fff;
    opacity: 0.0;
}
.mainNav li ul.dropDown li {
    float: left;
    height: 28px;
    line-height: 28px;
    margin: 0;
    padding: 0;
    font-size: 12px;
    color: #606060;
    /*font-weight: normal;*/

}
.mainNav li ul.dropDown li a {
    font-family: 'Raleway', sans-serif;
    font-weight: 400;
    line-height: 3em;
    height: 28px;
    display:block;
    padding: 0 20px;
    color: #606060;
}
.mainNav li ul.dropDown li a:hover {
    background: #CFCFCF;
}

.img-logo-coach-menu {
    position: absolute;
    margin-left: 5em;
}

.active {
    background: yellow;
}

2 个答案:

答案 0 :(得分:1)

将循环更改为

var url = window.location.pathname;
var urlRegExp = new RegExp(url.replace(/\/$/,''));
$('.mainNav li a').each(function(){
        // and test its href against the url pathname regexp
        if(urlRegExp.test(this.href)){
            $(this).addClass('active');
            $(this).closest('.mainNav>li').children('a').addClass("active");
        }
    });

closest函数找到与选择器匹配的第一个(最近的)祖先,然后您可以将该类添加到它的直接子项中。

答案 1 :(得分:1)

更改

$(this).addClass('active');

$(this).add($(this).parents('.mainNav li > a')).addClass('active');