jQuery on click on everything but a div and it's children

前端 未结 4 500
时光取名叫无心
时光取名叫无心 2021-01-11 18:38

I want to do something when I click anywhere, except when I click a div and it\'s children. This is what I\'ve tried so far, but it\'s not working (clicking on it\'s childr

4条回答
  •  春和景丽
    2021-01-11 19:35

    You could check inside the function for the element in question:

    $('body').click( function(e){
        if (!$(e.target).is("#calculator") || $("#calculator").has(e.target).length ) {
            // do your stuff
        }
    });
    

提交回复
热议问题