jQuery click off element event

后端 未结 15 1001
無奈伤痛
無奈伤痛 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: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.

提交回复
热议问题