jQuery click off element event

后端 未结 15 1003
無奈伤痛
無奈伤痛 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: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();
        };
    });
    

提交回复
热议问题