open and close submenus on click Jquery

前端 未结 2 1052
天涯浪人
天涯浪人 2021-01-18 11:21

I have a main menu that will display the submenus with a click event in jquery (client wanted to be click instead hover) so I got it working however I still can\'t figure ou

相关标签:
2条回答
  • 2021-01-18 11:34

    Just Add this line into your code

    $('#MainMenu > li').not(this).find('ul').slideUp();
    

    FULL CODE

    $('#MainMenu').find('> li').click(function() {
        $('#MainMenu > li').not(this).find('ul').slideUp();
        $(this).find('ul').stop(true, true).slideToggle(400);
        return false;
    });​
    

    Check Fiddle

    0 讨论(0)
  • 2021-01-18 11:48

    Try something like this

    $(function() {
        $('#MainMenu > li').click(function(e) { // limit click to children of mainmenue
            var $el = $('ul',this); // element to toggle
            $('#MainMenu > li > ul').not($el).slideUp(); // slide up other elements
            $el.stop(true, true).slideToggle(400); // toggle element
            return false;
        });
        $('#MainMenu > li > ul > li').click(function(e) {
            e.stopPropagation();  // stop events from bubbling from sub menu clicks
        });
    });​
    

    http://jsfiddle.net/Ssu32/

    0 讨论(0)
提交回复
热议问题