open and close submenus on click Jquery

前端 未结 2 1081
天涯浪人
天涯浪人 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: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/

提交回复
热议问题