Event on a click everywhere on the page outside of the specific div

后端 未结 4 1331
我在风中等你
我在风中等你 2020-12-03 12:23

I\'d like to hide a div when user click anywhere on the page outside of that div. How can I do that using raw javascript or jQuery?

4条回答
  •  生来不讨喜
    2020-12-03 12:48

    Attach a click event to the document to hide the div:

    $(document).click(function(e) {
       $('#somediv').hide();
    });
    

    Attach a click event to the div to stop clicks on it from propagating to the document:

    $('#somediv').click(function(e) {
       e.stopPropagation();
    });
    

提交回复
热议问题