Click outside div to hide div in pure JavaScript

前端 未结 6 1563
栀梦
栀梦 2020-12-11 02:50

I want to make a popup that should appear once a button is clicked and disappear once the user clicks outside of the box.

I\'m not sure how to make the div disappear

6条回答
  •  一个人的身影
    2020-12-11 03:16

    Here is my Solution.

    yourFunc=e=>{
      var popbox = document.getElementById("popbox");
      if(e.target.id !=="popbox"){
        popbox.style.display = "none";
        }else{
        popbox.style.display = "block";
        }
    }
    
    
    document.addEventListener("click",yourFunc)
    

提交回复
热议问题