如何在使用jquery单击模态外部时关闭模态?

如何在使用jquery单击模态外部时关闭模态?,jquery,Jquery,如何在使用jquery单击模态外部时关闭模态 $(document).ready(function () { $("body").click(function (e) { var x = e.pageX - e.target.offsetLeft; var y = e.pageY - e.target.offsetTop; if ((x < 75) || (x > 1275)) { $('.modal-backdrop').fadeOut

如何在使用jquery单击模态外部时关闭模态

$(document).ready(function () {

$("body").click(function (e) {
    var x = e.pageX - e.target.offsetLeft;
    var y = e.pageY - e.target.offsetTop;

    if ((x < 75) || (x > 1275)) {

        $('.modal-backdrop').fadeOut();
        $('.modal').fadeOut("fast");
    }

});
});
$(文档).ready(函数(){
$(“正文”)。单击(功能(e){
var x=e.pageX-e.target.offsetLeft;
变量y=e.pageY-e.target.offsetTop;
如果((x<75)|(x>1275)){
$('.modal background').fadeOut();
$('.modal')。淡出(“快速”);
}
});
});

您可以使用css来完成,例如:

var modal = document.getElementById('myModal');
window.onclick = function(event) {
   if (event.target == modal) {
        modal.style.display = "none";
    }
}