How to make modal close on click outside

后端 未结 7 700
误落风尘
误落风尘 2020-12-19 02:05

I have used this JavaScript below:

相关标签:
7条回答
  • 2020-12-19 02:59

    This is not the most efficient way, but it will work. The idea is to traverse the tree and check if the parent is the id of the one where you don't want to hide on click anywhere except it.

    $(document).on('click', function(e) {    
        var p = e.target;
        while(p) {
            console.log(p);
            if(p.id) {
                if(p.id == 'openModal') {
                    return;
                }
            }
            p = p.parentElement;
        }
        $("#openModal").hide();
    });
    
    0 讨论(0)
提交回复
热议问题