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

后端 未结 30 3422
庸人自扰
庸人自扰 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:12
    var n = 0;
    $("#container").mouseenter(function() {
    n = 0;
    
    }).mouseleave(function() {
    n = 1;
    });
    
    $("html").click(function(){ 
    if (n == 1) {
    alert("clickoutside");
    }
    });
    
    0 讨论(0)
  • 2020-11-21 05:13

    Return false if you click on .form_wrapper:

    $('body').click(function() {
      $('.form_wrapper').click(function(){
      return false
    });
       $('.form_wrapper').hide();
    });
    
    //$('.form_wrapper').click(function(event){
    //   event.stopPropagation();
    //});
    
    0 讨论(0)
  • 2020-11-21 05:13

    var exclude_div = $("#ExcludedDiv");;  
    $(document).click(function(e){
       if( !exclude_div.is( e.target ) )  // if target div is not the one you want to exclude then add the class hidden
            $(".myDiv1").addClass("hidden");  
    
    }); 

    FIDDLE

    0 讨论(0)
  • 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 <div> and listen to the focusout events.

    Setting tabindex will make sure that the blur event is fired on the <div> (normally it wouldn't).

    So your HTML would look like:

    <div class="form_wrapper" tabindex="0">
        <a class="agree" href="javascript:;">I Agree</a>
        <a class="disagree" href="javascript:;">Disagree</a>
    </div>
    

    And your JS:

    $('.form_wrapper').on('focusout', function(event){
        $('.form_wrapper').hide();
    });
    
    0 讨论(0)
  • 2020-11-21 05:14

    (Just adding on to prc322's answer.)

    In my case I'm using this code to hide a navigation menu that appears when the user clicks an appropriate tab. I found it was useful to add an extra condition, that the target of the click outside the container is not a link.

    $(document).mouseup(function (e)
    {
        var container = $("YOUR CONTAINER SELECTOR");
    
        if (!$("a").is(e.target) // if the target of the click isn't a link ...
            && !container.is(e.target) // ... or the container ...
            && container.has(e.target).length === 0) // ... or a descendant of the container
        {
            container.hide();
        }
    });
    

    This is because some of the links on my site add new content to the page. If this new content is added at the same time that the navigation menu disappears it might be disorientating for the user.

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

    Live DEMO

    Check click area is not in the targeted element or in it's child

    $(document).click(function (e) {
        if ($(e.target).parents(".dropdown").length === 0) {
            $(".dropdown").hide();
        }
    });
    

    UPDATE:

    jQuery stop propagation is the best solution

    Live DEMO

    $(".button").click(function(e){
        $(".dropdown").show();
         e.stopPropagation();
    });
    
    $(".dropdown").click(function(e){
        e.stopPropagation();
    });
    
    $(document).click(function(){
        $(".dropdown").hide();
    });
    
    0 讨论(0)
提交回复
热议问题