Use jQuery to hide a DIV when the user clicks outside of it

后端 未结 30 3517
庸人自扰
庸人自扰 2020-11-21 04:28

I am using this code:

$(\'body\').click(function() {
   $(\'.form_wrapper\').hide();
});

$(\'.form_wrapper\').click(function(event){
   event.stopPropagatio         


        
30条回答
  •  再見小時候
    2020-11-21 05:03

    This code detects any click event on the page and then hides the #CONTAINER element if and only if the element clicked was neither the #CONTAINER element nor one of its descendants.

    $(document).on('click', function (e) {
        if ($(e.target).closest("#CONTAINER").length === 0) {
            $("#CONTAINER").hide();
        }
    });
    

提交回复
热议问题