Javascript Detect click event outside of div

前端 未结 10 946
孤城傲影
孤城傲影 2020-11-27 06:07

I have a div with id=\"content-area\", when a user clicks outside of this div, I would like to alert them to the fact that they clicked outside of it. How would I use JavaSc

10条回答
  •  北海茫月
    2020-11-27 06:23

    use jquery as its best for DOM access

    $(document).click(function(e){
     if($(e.target).is("#content-area") || $(e.target).closest("#content-area").length)
      alert("inside content area");
    else alert("you clicked out side content area");
    });
    

提交回复
热议问题