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

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

    Copied from https://sdtuts.com/click-on-not-specified-element/

    Live demo http://demos.sdtuts.com/click-on-specified-element

    $(document).ready(function () {
        var is_specified_clicked;
        $(".specified_element").click(function () {
            is_specified_clicked = true;
            setTimeout(function () {
                is_specified_clicked = false;
            }, 200);
        })
        $("*").click(function () {
            if (is_specified_clicked == true) {
    //WRITE CODE HERE FOR CLICKED ON OTHER ELEMENTS
                $(".event_result").text("you were clicked on specified element");
            } else {
    //WRITE CODE HERE FOR SPECIFIED ELEMENT CLICKED
                $(".event_result").text("you were clicked not on specified element");
            }
        })
    })
    
    0 讨论(0)
  • 2020-11-21 05:16

    By using this code you can hide as many items as you want

    var boxArray = ["first element's id","second element's id","nth element's id"];
       window.addEventListener('mouseup', function(event){
       for(var i=0; i < boxArray.length; i++){
        var box = document.getElementById(boxArray[i]);
        if(event.target != box && event.target.parentNode != box){
            box.style.display = 'none';
        }
       }
    })
    
    0 讨论(0)
  • 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;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="modal-container active">
    	<div class="modal">
    		<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ac varius purus. Ut consectetur viverra nibh nec maximus. Nam luctus ligula quis arcu accumsan euismod. Pellentesque imperdiet volutpat mi et cursus. Sed consectetur sed tellus ut finibus. Suspendisse porttitor laoreet lobortis. Nam ut blandit metus, ut interdum purus.</p>
    	</div>
    </div>

    0 讨论(0)
  • 2020-11-21 05:18
    dojo.query(document.body).connect('mouseup',function (e)
    {
        var obj = dojo.position(dojo.query('div#divselector')[0]);
        if (!((e.clientX > obj.x && e.clientX <(obj.x+obj.w)) && (e.clientY > obj.y && e.clientY <(obj.y+obj.h))) ){
            MyDive.Hide(id);
        }
    });
    
    0 讨论(0)
  • 2020-11-21 05:19
     $('body').click(function(event) {
        if (!$(event.target).is('p'))
        {
            $("#e2ma-menu").hide();
        }
    });
    

    p is the element name. Where one can pass the id or class or element name also.

    0 讨论(0)
  • 2020-11-21 05:23

    Live demo with ESC functionality

    Works on both Desktop and Mobile

    var notH = 1,
        $pop = $('.form_wrapper').hover(function(){ notH^=1; });
    
    $(document).on('mousedown keydown', function( e ){
      if(notH||e.which==27) $pop.hide();
    });
    

    If for some case you need to be sure that your element is really visible when you do clicks on the document: if($pop.is(':visible') && (notH||e.which==27)) $pop.hide();

    0 讨论(0)
提交回复
热议问题