jQuery Close DIV By Clicking Anywhere On Page

后端 未结 5 765
夕颜
夕颜 2021-01-13 11:07

I would like to open email-signup when I click on email-signup-link. Then I would like to close it by clicking anywhere on the page except for the

相关标签:
5条回答
  • 2021-01-13 11:17

    The way I've often seen this done is by overlaying the page behind the form with a div (greyed out usually). With that, you could use:

    $("#greydiv")..click(function() {
        $("#email-signup").hide();
        $("#greydiv").hide();
    });
    

    ...or something simliar.

    0 讨论(0)
  • 2021-01-13 11:19
    var mouse_is_inside = false;
    
    $(document).ready(function()
    {
        $('.form_content').hover(function(){ 
            mouse_is_inside=true; 
        }, function(){ 
            mouse_is_inside=false; 
        });
    
        $("body").mouseup(function(){ 
            if(! mouse_is_inside) $('.form_wrapper').hide();
        });
    });
    

    as referenced in another stackoverflow post...

    0 讨论(0)
  • 2021-01-13 11:26
    $(":not(#email-signup)").click(function() {
         $("#email-signup").hide();
    });
    

    Although you'd be better off having some kind of an overlay behind the popup and binding the above click event to that only.

    0 讨论(0)
  • 2021-01-13 11:36
    $(document).click (function (e) {
        if (e.target != $('#email-signup')[0]) {
            $('#email-signup').hide();
        }
    });
    
    0 讨论(0)
  • 2021-01-13 11:39

    Two things. You don't actually have e defined, so you can't use it. And you need stopPropagation in your other click handler as well:

    $('#email-signup').click(function(e){
        e.stopPropagation();
    });
    $("#email-signup-link").click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        $('#email-signup').show();
    });
    $(document).click(function() {
        $('#email-signup').hide();
    });​
    

    http://jsfiddle.net/Nczpb/

    0 讨论(0)
提交回复
热议问题