Use jQuery to hide a DIV when the user clicks outside of it

后端 未结 30 3512
庸人自扰
庸人自扰 2020-11-21 04:28

I am using this code:

$(\'body\').click(function() {
   $(\'.form_wrapper\').hide();
});

$(\'.form_wrapper\').click(function(event){
   event.stopPropagatio         


        
30条回答
  •  广开言路
    2020-11-21 05:02

    And for Touch devices like IPAD and IPHONE we can use following code

    $(document).on('touchstart', function (event) {
    var container = $("YOUR CONTAINER SELECTOR");
    
    if (!container.is(e.target) // if the target of the click isn't the container...
    && container.has(e.target).length === 0) // ... nor a descendant of the container
        {
            container.hide();
        }
    });
    

提交回复
热议问题