javascript 从另一个功能打开弹出窗口 - Magnific Popup

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21928177/
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 22:01:51  来源:igfitidea点击:

Open popup from another function - Magnific Popup

javascriptjquerypopupmagnific-popup

提问by Hassan Sardar

I want to open my popup when ajaxCall function is called.

我想在调用 ajaxCall 函数时打开我的弹出窗口。

Like this:

像这样:

function ajaxCall()
{

 openPopup();

}

function openPopup()
{

$('.popup-modal').magnificPopup({

type: 'inline',
modal: false,

});

$(document).on('click', '.closePopup', function (e) 
            {
                e.preventDefault();
                $.magnificPopup.close();
            });

}

Here is the fiddle:http://jsfiddle.net/qweWa/33/

这是小提琴:http : //jsfiddle.net/qweWa/33/

I want this popup to open when ajaxCall function is called.

我希望在调用 ajaxCall 函数时打开此弹出窗口。

Here is the working fiddle for onclick button open popup:

这是 onclick 按钮打开弹出窗口的工作小提琴:

http://jsfiddle.net/qweWa/27/

http://jsfiddle.net/qweWa/27/

回答by crafter

Use the open() function

使用 open() 函数

function openPopup(el) { // get the class name in arguments here
            $.magnificPopup.open({
                items: {
                    src: '#thanksModal',
                },
                type: 'inline'
            });
}

JSFiddle : http://jsfiddle.net/t5f5e5zw/2/

JSFiddle:http: //jsfiddle.net/t5f5e5zw/2/

回答by Jai

You have to pass the class name of clicked button in the function:

您必须在函数中传递单击按钮的类名:

function openPopup(el) { // get the class name in arguments here
   $('.'+el).magnificPopup({ // use it here
      type: 'inline',
      modal: false
   });
}

$(function () {
   $('.ajax-call').click(function (e) {
      openPopup(this.className); //<----pass the clicked button's class name
   });
   $(document).on('click', '.closePopup', function (e) {
      e.preventDefault();
      $.magnificPopup.close();
   });
});

Demo Fiddle

演示小提琴

回答by Java_User

magnificPopup is not a functionerror. Please include any custom js files in your header section.

magnificPopup is not a function错误。请在标题部分包含任何自定义 js 文件。

回答by Dimag Kharab

Try this

试试这个

function openPopup()
        {

            $('.ajax-call').magnificPopup({

            type: 'inline',
            modal: true,

        });

        /*$.magnificPopup({



 type: 'inline',
        modal: false,

    });*/

    $(document).on('click', '.closePopup', function (e) 
                {
                    e.preventDefault();
                    $.magnificPopup.close();
                });

    }