[removed] Click anywhere in body except the one element inside it

前端 未结 4 807
温柔的废话
温柔的废话 2021-02-13 13:43

I want to be able to click anywhere inside the body except that one specific element. I can\'t find out what\'s wrong with the code I have done.

When I click on the one

4条回答
  •  盖世英雄少女心
    2021-02-13 14:24

    Instead of stopping propagation on the except element, I'd prefer something like this:

    var wrapper = document.querySelector('wrapper');  
    
    document.addEventListener('click', function(e) {
       if ( !wrapper.contains(e.target) ) {
           // Do something when user clicked outside of wrapper element
       }
    })
    

提交回复
热议问题