Close a div by clicking outside

前端 未结 7 995
感情败类
感情败类 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: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);
                        }
            });
        });
     });
    

提交回复
热议问题