jQuery: Close DIV by clicking anywhere apart from the DIV itself?

后端 未结 3 1911

I have a div that slides down when a button is clicked, and I would like to slide up the div when a user:

  1. Clicks anwhere except within the DIV itself
3条回答
  •  清歌不尽
    2020-12-16 14:12

    EDIT How to create a custom modal popup - and how to close it clicking outside of it


    Here, just an example of mine: http://jsbin.com/uqiver/1/edit

    $("body").on('click', function(ev) {
        var myID = ev.target.id;
        if (myID !== 'panel') {
            $('#panel').hide();
        }
    });
    

    Or you can also check if it's visible at first:

    var $pan = $('#panel');
    
    $("body").on('click', function(ev) {
        if ($pan.is(':visible') && ev.target.id !== 'panel') $pan.hide();
    });
    

提交回复
热议问题