How to close a modal by clicking outside the modal window?

偶尔善良 提交于 2019-11-30 23:38:12

问题


In a very simple jQuery modal, I close the modal by clicking on CLOSE as

$('#close').click(function(e) {
  e.preventDefault();
  $('#overlay, #alertModalOuter').fadeOut(400, function() {
     $(this).remove();
  });
});

How can I close the modal by clicking whether on CLOSE button (which is inside the modal windows) OR clicking anywhere outside the modal window.


回答1:


Changing your function like so should work:

    $('#close, #overlay').click(function(e) {
      e.preventDefault();
      $('#overlay, #alertModalOuter').fadeOut(400, function() {
      $('#close').remove();
    });
});



回答2:


I found it helpful to include:

$('.item-modal').click(function(e) {
  e.stopPropagation();
});



回答3:


Add the same click listener to your overlay.



来源:https://stackoverflow.com/questions/8152819/how-to-close-a-modal-by-clicking-outside-the-modal-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!