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

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

    $(document).ready(function() {
    	$('.modal-container').on('click', function(e) {
    	  if(e.target == $(this)[0]) {
    		$(this).removeClass('active'); // or hide()
    	  }
    	});
    });
    .modal-container {
    	display: none;
    	justify-content: center;
    	align-items: center;
    	position: absolute;
    	top: 0;
    	left: 0;
    	right: 0;
    	bottom: 0;
    	background-color: rgba(0,0,0,0.5);
    	z-index: 999;
    }
    
    .modal-container.active {
        display: flex;  
    }
    
    .modal {
    	width: 50%;
    	height: auto;
    	margin: 20px;
    	padding: 20px;
    	background-color: #fff;
    }
    
    

提交回复
热议问题