开发者

Add class to parent li if it has a sub ul - using jquery

I'd like to be able to add a cla开发者_运维百科ss to my top most li, but only if it has a sub ul or ul ul. Each li contains a link and yes, it's a drop down navigation menu.

My current jquery:

$(function() {
    $('ul.sub-menu').hide();
    $('.sub-menu ul').hide();

    $('#menu-navigation li').hover(function(){
        $(this).children('ul').slideDown("fast");
        },
    function(){
        $(this).children('ul').hide();
    });

    $('#menu-navigation ul li').hover(function(){
        $(this).children('ul').slideDown("fast");
        },
    function(){
        $(this).children('ul').hide();
    });
});

This only shows / hides each element.

I'd like the top-most li to have a class, so that when I hover over the child elements, that class remains in place. How would I do this?

Thanks in advance for any help.


Try this

$(function() {
    $('ul.sub-menu').hide();
    $('.sub-menu ul').hide();

    $('#menu-navigation li').hover(function(){
        $(this).children('ul').slideDown("fast");
        },
    function(){
        $(this).children('ul').hide();
    });

    $('#menu-navigation > ul > li').hover(function(){
        $(this).children('ul').slideDown("fast");
    },
    function(){
        $(this).children('ul').hide();
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜