在条件语句中调用模态窗口

Calling a modal windows in a conditional statement

本文关键字:模态 窗口 调用 条件 语句      更新时间:2023-09-26

我有这个脚本的工作原理上的按钮点击,它弹出一个模态窗口与褪色的背景。这次我想要实现的是将其包含在条件语句中,脚本的后一部分如下

 if (result != $version) { alert ('Your version is out of date') } } })
alert ('You are welcome back')

但是我想将警报更改为模态窗口,但无法通过

找到我的方式
// JavaScript Document

$(document).on("pagecreate", function () {
    $(".about").on("click", function () {
        // close button
        var closeBtn = $('<a href="#" data-rel="back" class="ui-btn-right ui-btn ui-btn-b ui-corner-all ui-btn-icon-notext ui-icon-delete ui-shadow">Close</a>');
        // text you get from Ajax
        var content = "<p> hello folks, good evening</p>";
        // Popup body - set width is optional - append button and Ajax msg
        var popup = $("<div/>", {
            "data-role": "popup"
        }).css({
            width: $(window).width() / 0 + "px",
            padding: 5 + "px"
        }).append(closeBtn).append(content);
        // Append it to active page
        $.mobile.pageContainer.append(popup);
        // Create it and add listener to delete it once it's closed
        // open it
        $("[data-role=popup]").popup({
            dismissible: false,
            history: false,
            theme: "b",
            /* or a */
            positionTo: "window",
            overlayTheme: "b",
            /* "b" is recommended for overlay */
            transition: "pop",
            beforeposition: function () {
                $.mobile.pageContainer.pagecontainer("getActivePage")
                    .addClass("blur-filter");
            },
            afterclose: function () {
                $(this).remove();
                $(".blur-filter").removeClass("blur-filter");
            },
            afteropen: function () {
                /* do something */
            }
        }).popup("open");
    });
});

最简单的方法就是在条件语句中触发点击:

if (result != $version) { 
    $(".about").trigger("click"); 
}

然而,我将把点击回调重构为一个命名函数,并能够在条件和按钮的两个实例上调用它:

var openModal = function () {
        // close button
        var closeBtn = $('<a href="#" data-rel="back" class="ui-btn-right ui-btn ui-btn-b ui-corner-all ui-btn-icon-notext ui-icon-delete ui-shadow">Close</a>');
        // text you get from Ajax
        var content = "<p> hello folks, good evening</p>";
        // Popup body - set width is optional - append button and Ajax msg
        var popup = $("<div/>", {
            "data-role": "popup"
        }).css({
            width: $(window).width() / 0 + "px",
            padding: 5 + "px"
        }).append(closeBtn).append(content);
        // Append it to active page
        $.mobile.pageContainer.append(popup);
        // Create it and add listener to delete it once it's closed
        // open it
        $("[data-role=popup]").popup({
            dismissible: false,
            history: false,
            theme: "b",
            /* or a */
            positionTo: "window",
            overlayTheme: "b",
            /* "b" is recommended for overlay */
            transition: "pop",
            beforeposition: function () {
                $.mobile.pageContainer.pagecontainer("getActivePage")
                    .addClass("blur-filter");
            },
            afterclose: function () {
                $(this).remove();
                $(".blur-filter").removeClass("blur-filter");
            },
            afteropen: function () {
                /* do something */
            }
        }).popup("open");
};

$(document).on("pagecreate", function () {
    $(".about").on("click", openModal);
});

条件句:

if (result != $version) { 
    openModal(); 
}