Close a menu with an anywhere click

后端 未结 5 1994
无人共我
无人共我 2021-01-06 17:18

As you guys can see, I have a drop down menu.

I have a lot of columns, each one has an option to open the menu.

$(\".optionCont\").live(\"click\", fu         


        
5条回答
  •  天命终不由人
    2021-01-06 17:59

    Try something like:

    $(document).click(function(e) {
       if ($(e.target) != myEl)
           myEl.slideUp();
    })
    

    Alternative: working example.

    Source:

    $(".optionCont").live("click", function(e) {
        var that = this;
        $(document).click(function(e) {
            console.log(e.target);
            console.log(that);
            if (e.target != that) {
                e.stopPropagation();
                e.preventDefault();
                $(".dropMenuCont").slideUp();
            }
        });
        if ($(this).next().css("display") === "none") {
            $(this).next().slideDown();
        } else {
            $(this).next().slideUp();
        }
        e.stopPropagation();
        e.preventDefault();
    });
    

提交回复
热议问题