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

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

    Attach a click event to top level elements outside the form wrapper, for example:

    $('#header, #content, #footer').click(function(){
        $('.form_wrapper').hide();
    });
    

    This will also work on touch devices, just make sure you don't include a parent of .form_wrapper in your list of selectors.

提交回复
热议问题