javascript 关闭时的华丽弹出回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17101586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:05:48  来源:igfitidea点击:

Magnific Popup Callback when closing

javascriptjqueryhtmlmagnific-popup

提问by Mifas

I am using Magnific Popupfor uploading images. When the user clicks or presses the close button, I'd like to get confirmation from the user whether to close on not.

我正在使用Magnific Popup上传图片。当用户点击或按下关闭按钮时,我想得到用户的确认是否关闭。

This is my Javascript:

这是我的Javascript:

$('#upload').magnificPopup({
    type:'inline',
    callbacks: {
        close: function(){
            if( confirm("Are you sure you want to close?") ) {
              return true;
            }
              return false;
            }
          }
      }
});

But it is not working.

但它不起作用。

回答by Michael Irigoyen

You can override the close method. By modifying the instance, you will only change the functionality of this specific popup. Then simply call the original closemethod to finish the job.

您可以覆盖 close 方法。通过修改instance,您只会更改此特定弹出窗口的功能。然后只需调用原始close方法即可完成工作。

$('#upload').magnificPopup({
  type:'inline',
  callbacks: {
    open: function() {
      $.magnificPopup.instance.close = function() {
        // Do whatever else you need to do here
        var confirmed = confirm("Are you sure you want to close?");
        if(!confirmed) {
          return;
        }

        // Call the original close method to close the popup
        $.magnificPopup.proto.close.call(this);
      };
    }
  }
});

回答by CodeHunter

u may try this

你可以试试这个

('#upload').magnificPopup({
    type:'inline',
    callbacks: {
      close: function(){
         var didConfirm = confirm("Are you sure?");
         if(didConfirm ==false){
            return false;
         }
      }
  });