开发者

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

开发者 https://www.devze.com 2023-03-25 15:28 出处:网络
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.

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

精彩评论

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