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

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

    Instead of listening to every single click on the DOM to hide one specific element, you could set tabindex to the parent

    and listen to the focusout events.

    Setting tabindex will make sure that the blur event is fired on the

    (normally it wouldn't).

    So your HTML would look like:

    
    

    And your JS:

    $('.form_wrapper').on('focusout', function(event){
        $('.form_wrapper').hide();
    });
    

提交回复
热议问题