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

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

    Even sleaker:

    $("html").click(function(){ 
        $(".wrapper:visible").hide();
    });
    
    0 讨论(0)
  • 2020-11-21 05:08

    Wouldn't something like this work?

    $("body *").not(".form_wrapper").click(function() {
    
    });
    

    or

    $("body *:not(.form_wrapper)").click(function() {
    
    });
    
    0 讨论(0)
  • 2020-11-21 05:08

    Built off of prc322's awesome answer.

    function hideContainerOnMouseClickOut(selector, callback) {
      var args = Array.prototype.slice.call(arguments); // Save/convert arguments to array since we won't be able to access these within .on()
      $(document).on("mouseup.clickOFF touchend.clickOFF", function (e) {
        var container = $(selector);
    
        if (!container.is(e.target) // if the target of the click isn't the container...
            && container.has(e.target).length === 0) // ... nor a descendant of the container
        {
          container.hide();
          $(document).off("mouseup.clickOFF touchend.clickOFF");
          if (callback) callback.apply(this, args);
        }
      });
    }
    

    This adds a couple things...

    1. Placed within a function with a callback with "unlimited" args
    2. Added a call to jquery's .off() paired with a event namespace to unbind the event from the document once it's been run.
    3. Included touchend for mobile functionality

    I hope this helps someone!

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

    A solution without jQuery for the most popular answer:

    document.addEventListener('mouseup', function (e) {
        var container = document.getElementById('your container ID');
    
        if (!container.contains(e.target)) {
            container.style.display = 'none';
        }
    }.bind(this));
    

    MDN: https://developer.mozilla.org/en/docs/Web/API/Node/contains

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

    What you can do is bind a click event to the document that will hide the dropdown if something outside the dropdown is clicked, but won't hide it if something inside the dropdown is clicked, so your "show" event (or slidedown or whatever shows the dropdown)

        $('.form_wrapper').show(function(){
    
            $(document).bind('click', function (e) {
                var clicked = $(e.target);
                if (!clicked.parents().hasClass("class-of-dropdown-container")) {
                     $('.form_wrapper').hide();
                }
            });
    
        });
    

    Then when hiding it, unbind the click event

    $(document).unbind('click');
    
    0 讨论(0)
  • 2020-11-21 05:11

    i did it like this:

    var close = true;
    
    $(function () {
    
        $('body').click (function(){
    
            if(close){
                div.hide();
            }
            close = true;
        })
    
    
    alleswasdenlayeronclicknichtschliessensoll.click( function () {   
            close = false;
        });
    
    });
    
    0 讨论(0)
提交回复
热议问题