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

后端 未结 30 3435
庸人自扰
庸人自扰 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:02

    You'd better go with something like this:

    var mouse_is_inside = false;
    
    $(document).ready(function()
    {
        $('.form_content').hover(function(){ 
            mouse_is_inside=true; 
        }, function(){ 
            mouse_is_inside=false; 
        });
    
        $("body").mouseup(function(){ 
            if(! mouse_is_inside) $('.form_wrapper').hide();
        });
    });
    

提交回复
热议问题