jQuery click off element event

后端 未结 15 993
無奈伤痛
無奈伤痛 2020-12-13 00:25

I have a floating div that gets displayed, and I want it to be hidden when the user clicks off the div. This would be similar to the .hover() function callback when hoverin

相关标签:
15条回答
  • 2020-12-13 00:37

    Surely you're looking for the blur event?

    0 讨论(0)
  • 2020-12-13 00:38

    You can try this. http://benalman.com/projects/jquery-clickoutside-plugin/

    0 讨论(0)
  • 2020-12-13 00:38

    Using an event handler on the document works well for me:

    function popUp( element )
    {
        element.onmousedown = function (event) { event.stopPropagation(); };
        document.onmousedown = function () { popDown( element ); };
    
        document.body.appendChild( element );
    }
    
    function popDown( element )
    {
        document.body.removeChild( element );
    
        document.onmousedown = null;
    }
    
    0 讨论(0)
  • 2020-12-13 00:46

    If you want to clear the div when you click somewhere else in the page, you can do something like:

    $('body').click(function(event) {
        if (!$(event.target).closest('#myDiv').length) {
            $('#myDiv').hide();
        };
    });
    
    0 讨论(0)
  • 2020-12-13 00:47
         $('body').click(function (event) {        
    if ($("#divID").is(':visible')) {
                $('#divID').slideUp();
            }
    });
    

    This can be used to check if the div is visible, if it is visible, it will then slide the object up.

    0 讨论(0)
  • 2020-12-13 00:47

    I've found the solution in a forum... but I can't find it back to credit the orginal author. Here is the version (modified that lives in my code).

     $(document).bind('mousedown.yourstuff', function(e) {
                var clicked=$(e.target); // get the element clicked                 
                if( clicked.is('#yourstuff')
                     || clicked.parents().is('#yourstuff')) {
                    // click safe!
                } else {
                    // outside click
                    closeIt();
                }
            });
    
     function closeIt() {
            $(document).unbind('mousedown.emailSignin');
            //...
    }
    

    I also have ESC keyup bindings and a 'close' html anchor not pictured above.

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