Close a div by clicking outside

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

提交回复
热议问题