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

前端 未结 4 483
时光取名叫无心
时光取名叫无心 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:22

    Instead of

    $('body').on('click', '* :not(#calculator)',  function(e){
    
    });​
    

    You can use (this version is preferred as it is more performant)

    $("body").on("click", ":not(#calculator, #calculator *)", function(e){ 
    
    });​
    

    or

    $("body :not(#calculator, #calculator *)").on("click", function(e){ 
    
    });​
    

提交回复
热议问题