undefined 不是 magnificpopup.close 的对象

undefined is not an object with magnificpopup.close

我在使用 magnificPopup 的弹出窗口中有一个自定义关闭按钮。关闭按钮在 FireFox 中有效,但在 Chrome 或 Safari 中无效。在 Safari 中,我收到以下错误:

undefined is not an object (evaluating 'jQuery.magnificPopup.close')

这是我的 JS:

jQuery(document).ready(function($) {
    setTimeout(function() {
        if ($('#cookies-message').length) {
            $.magnificPopup.open({
                items: {
                    src: '#cookies-message' 
                },
                type: 'inline'
            });
        }
    }, 1000);
});

jQuery(document).on('click', '.popup-modal-dismiss', function (e) {
    //e.preventDefault();
    jQuery.magnificPopup.close();
});
<div id="cookies-message" class="cookie-message mfp-hide">
    <img src="/wp-content/themes/site/img/cookies.svg">
    <p>… but we also use them to give you the best experience on our website, which is full of other healthy solutions</p>
    <p><a class="popup-modal-dismiss" href="#">OKIDOKI</a></p>
</div>

这样试试

$.magnificPopup.close();

var magnificPopup = $.magnificPopup.instance; // save instance in magnificPopup variable
magnificPopup.close(); // Close popup that is currently opened

参考this

您可能在加载 jQuery 之前加载 magnificPopup,在这种情况下,它在 jQuery 对象上不可用。

您应该能够使用 $.magnificPopup.

安全地访问 magnificPopup