Detect click outside div using javascript

后端 未结 7 1882
無奈伤痛
無奈伤痛 2020-12-02 13:10

I\'d like to detect a click inside or outside a div area. The tricky part is that the div will contain other elements and if one of the elements inside the div is clicked, i

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 13:45

    I came up with a hack for this that's working well for me and that might help others.

    When I pop up my dialog DIV, I simultaneously display another transparent DIV just behind it, covering the whole screen.

    This invisible background DIV closes the dialog DIV onClick.

    This is pretty straightforward, so I'm not going to bother with the code here. LMK in the comments if you want to see it and I'll add it in.

    HTH!

提交回复
热议问题