weixin_33709609 2014-09-16 23:44 采纳率: 0%
浏览 22

jQuery调用函数

I have am having an issue with my modal window structure. I have a div that calls a function when it is clicked to show a modal window. Inside of the showModal() function, I call another function that acts as the close function if the user clicks ESC key or out of the modal window anywhere in the document.

Here is the code:

$('body').on('click', '.container', function(){
    showModal();
});

On click, this opens up showModal

function showModal(_this) {
  $('body').addClass('video-open');
  $('.popup').load(templateDir + '/views/video.html', function() {
   $(this).show();
   $('.cloaked').show();
 });
 closeModal('.modal-window');
}

closeModal gets called immediately in the showModal. For some reason emptyModal gets fired immediately as well when my intention is to only fire it when the user clicks ESC or out of the modal.

function closeModal(selector) {
 $(document).keyup(function(e){
  if (e.keyCode == 27) {
   emptyModal(selector); 
  } 
});

$(document).click(function(e) {
 if (!$(e.target).closest(selector).length) {
   emptyModal(selector);      
  }
 });  
}

Using console.log I can see that 'window closed' and confirm that emptyModal is getting called immediately when closeModal is even though it is wrapped in conditional statements.

function emptyModal(selector) {
 console.log('window closed')
 $(selector).empty().hide();   
 $('.cloaked').hide();
}

I think overall I have a misunderstanding of how to do what I need and I am probably implementing it wrong. Just a side note, I have closeModal and emptyModal as separate functions as I use them in different modal view functions other than the showModal one.

  • 写回答

1条回答 默认 最新

  • weixin_33743661 2014-09-16 23:55
    关注

    In your closeModal function , instead of putting a click event on the whole document try putting it on .cloaked

    $('.cloaked').click(function(e) {
     if (!$(e.target).closest(selector).length) {
       emptyModal(selector);      
      }
     }); 
    

    If I understand correctly , the element with the .cloaked class is supposed to show a black cloak in the background when the modal dialog opens up, so clicking any where other than the modal dialog would mean that the user is clicking on the element with the .cloaked class.

    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同