单击显示子菜单

时间:2015-06-02 12:17:34

标签: javascript jquery css menu click

我的菜单正在进行悬停。 当我悬停在第一级时,它会显示第二级菜单。

我想更改此设置,以便当我点击第一级时,它会显示第二级并保持可见。

This is my code on jsfiddle

<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>
$(function () {

    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");
        }
    });

});

$("ul.mainNav li").hover(function () {
    $(this).find("ul.dropDown").css({ "display": "block" }).fadeTo('500', '1.0');
}, function () {
    $(this).find("ul.dropDown").fadeTo('200', '0.0').css({ "display": "none" });
});

我的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;
    text-transform: uppercase;
}

.mainNav li:hover > a {
    color: #ffc102;
}

.mainNav li a:hover{
    color: #ffc102;
}

.mainNav li ul.dropDown {
    margin:0;
    padding:0;
    position: absolute;
    display: none;
    background: #fff;
    opacity: 0.0;
    height: 3em;
}
.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: 4em;
    height: 28px;
    display:block;
    padding: 0 20px;
    color: #606060;
    text-transform: uppercase;
}
.mainNav li ul.dropDown li a:hover {
    color: #ffc102;
}

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

.active {
    background: #DCDCDC
    color: #ffc102;
    /*opacity: 1;*/
    /*visibility: visible;*/
}

我试图做那样的事情

    $('.mainNav li a').click(function(){
       $('.mainNav li a ul.dropDown').hide();

        $(this).closest(" .dropDown" ).css({"display" : "block"});
        $(this).closest(" .dropDown" ).css({"opacity" : 1});
       $(this).next().show();

    });

但它不起作用。

1 个答案:

答案 0 :(得分:0)

Mike你没有正确使用你的选择器,jquery总是依赖于你的DOM结构,所以你的代码可能是正确的,但它可能不会给你想要的结果。

我刚刚更新了http://jsfiddle.net/mst0057o/4/

代码正确如下

$('.mainNav > li a').click(function(e){
            e.preventDefault();
            $(this).next("ul.dropDown").css({"display" : "block"}).fadeTo('500', '1.0');
        });

其余的部分你需要注意,我只是更新了点击功能。

相关问题