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

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

    Use not with a comma to have both selectors: the element itself and the elements children

    jQuery(document.body).on("click", ":not(#calculator, #calculator *)", function(e){ 
        console.log(this); 
        e.stopPropagation(); 
    });​​​​​​​
    

    jsFiddle

提交回复
热议问题