Close a div by clicking outside

前端 未结 7 952
感情败类
感情败类 2020-11-27 06:05

I want to hide a div by clicking on the close link in it, or by clicking anywhere outside that div.

I am trying following code, it opens and close t

相关标签:
7条回答
  • 2020-11-27 06:39

    An other way which makes then your jsfiddle less buggy (needed double click on open).

    This doesn't use any delegated event to body level

    Set tabindex="-1" to DIV .popup ( and for style CSS outline:0 )

    DEMO

    $(".link").click(function(e){
        e.preventDefault();
        $(".popup").fadeIn(300,function(){$(this).focus();});
    });
    
    $('.close').click(function() {
       $(".popup").fadeOut(300);
    });
    $(".popup").on('blur',function(){
        $(this).fadeOut(300);
    });
    
    0 讨论(0)
  • 2020-11-27 06:39

    You need

    $('body').click(function(e) {
        if (!$(e.target).closest('.popup').length){
            $(".popup").hide();
        }
    });
    
    0 讨论(0)
  • 2020-11-27 06:43

    You'd better go with something like this. Just give an id to the div which you want to hide and make a function like this. call this function by adding onclick event on body.

    function myFunction(event) { 
    
    if(event.target.id!="target_id")
    { 
        document.getElementById("target_id").style.display="none";
      }
    }
    
    0 讨论(0)
  • 2020-11-27 06:50

    I'd suggest using the stopPropagation() method as shown in the modified fiddle:

    Fiddle

    $('body').click(function() {
        $(".popup").hide();
    });
    
    $('.popup').click(function(e) {
        e.stopPropagation();
    });
    

    That way you can hide the popup when you click on the body, without having to add an extra if, and when you click on the popup, the event doesn't bubble up to the body by going through the popup.

    0 讨论(0)
  • 2020-11-27 06:56
     //for closeing the popover when user click outside it will close all popover 
     var hidePopover = function(element) {
            var elementScope = angular.element($(element).siblings('.popover')).scope().$parent;
            elementScope.isOpen = false;
            elementScope.$apply();
            //Remove the popover element from the DOM
            $(element).siblings('.popover').remove();
        };
     $(document).ready(function(){
     $('body').on('click', function (e) {
           $("a").each(function () {
                        //Only do this for all popovers other than the current one that cause this event
               if (!($(this).is(e.target) || $(this).has(e.target).length > 0) 
                    && $(this).siblings('.popover').length !== 0 && $(this).siblings('.popover').has(e.target).length === 0)                  
                        {
                             hidePopover(this);
                        }
            });
        });
     });
    
    0 讨论(0)
  • 2020-11-27 06:59

    This question might have been answered here. There might be some potential issues when event propagation is interrupted, as explained by Philip Walton in this post.

    A better approach/solution would be to create a custom jQuery event, e.g. clickoutside. Ben Alman has a great post (here) that explains how to implement one (and also explains how special events work), and he's got a nice implementation of it (here).

    More reading on jQuery events API and jQuery special events:

    • Introduction to custom events
    • jQuery event extensions
    0 讨论(0)
提交回复
热议问题