Use jQuery to hide a DIV when the user clicks outside of it

后端 未结 30 4547
庸人自扰
庸人自扰 2020-11-21 04:28

I am using this code:

$(\'body\').click(function() {
   $(\'.form_wrapper\').hide();
});

$(\'.form_wrapper\').click(function(event){
   event.stopPropagatio         


        
30条回答
  •  南旧
    南旧 (楼主)
    2020-11-21 05:15

    Copied from https://sdtuts.com/click-on-not-specified-element/

    Live demo http://demos.sdtuts.com/click-on-specified-element

    $(document).ready(function () {
        var is_specified_clicked;
        $(".specified_element").click(function () {
            is_specified_clicked = true;
            setTimeout(function () {
                is_specified_clicked = false;
            }, 200);
        })
        $("*").click(function () {
            if (is_specified_clicked == true) {
    //WRITE CODE HERE FOR CLICKED ON OTHER ELEMENTS
                $(".event_result").text("you were clicked on specified element");
            } else {
    //WRITE CODE HERE FOR SPECIFIED ELEMENT CLICKED
                $(".event_result").text("you were clicked not on specified element");
            }
        })
    })
    

提交回复
热议问题