代码之家  ›  专栏  ›  技术社区  ›  jegtugado

无法关闭淡入淡出的引导模式

  •  1
  • jegtugado  · 技术社区  · 7 年前

    我正在使用以下代码动态生成模式弹出窗口,但遇到了一个问题。问题是模式显示正确,但我无法关闭它。我都试过了 data-dismiss="modal" 和 .modal("hide") 无济于事。我检查过按钮单击事件是否被触发,唯一的问题是模式没有关闭。

    JS

    // Requires jQuery
    var dialogHelper = new dialog();
    
    function dialog() {
        /* Bootstrap Modal Dialog
        *  Displays message from parameter
        */
        this.ShowModalDialog = function (message, title, buttons) {
            var dialogMessage = "";
            var dialogTitle = "System Message";
            var dialogButtons = [];
    
            if (message)
                dialogMessage = message;
            if (title)
                dialogTitle = title;
            if (buttons) {
                dialogButtons = buttons;
            }
    
            var id = randomString(10);
    
            jQuery("<div/>", {
                id: id,
                class: "modal fade",
                // href: 'http://google.com',
                //title: title,
                //rel: 'external',
                //text: message
            })
                .attr("tabindex", "-1")
                .attr("role", "dialog")
                .attr("aria-labelledby", id + "Label")
                .attr("aria-hidden", true)
                .attr("data-backdrop", "static")
                .attr("data-keyboard", false)
                .load("/Static/BootstrapDialogTemplate.html", function () {
                    $("#" + id + " .modal-title")
                        .attr("id", id + "Label")
                        .text(dialogTitle);
                    $("#" + id + " .modal-body").text(dialogMessage);
    
                    var footer = $("#" + id + " .modal-footer");
    
                    dialogButtons.forEach(function (element) {
                        $('<button/>', {
                            text: element.Text,
                            click: function () {
                                if (element.Event) {
                                    element.Event();
                                }
                            },
                            class: element.Class
                        })
                            .attr("data-dismiss", "modal")
                            .appendTo(footer);
                    });
                })
                .appendTo(document.body)
                .modal("show");
        };
    };
    
    /* Automatically destroy modal on close */
    $(".modal").on('hidden.bs.modal', function () {
        $(this).data('bs.modal', null);
    });
    
    function randomString(length) {
        var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
        var string_length = length;
        var randomstring = '';
    
        for (var i = 0; i < string_length; i++) {
            var rnum = Math.floor(Math.random() * chars.length);
            randomstring += chars.substring(rnum, rnum + 1);
        }
    
        return randomstring;
    };
    

    /静态/bootstrapdialogtemplate.html

    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
    

    样本调用

    dialogHelper.ShowModalDialog("Are you sure you want to create this item?", null, [{
            Text: "Yes",
            Event: function () { alert("YES!"); },
            Class: "btn btn-primary"
        }, {
            Text: "No",
            Event: function () { },
            Class: "btn btn-secondary"
        }]);
    

    样本输出

    <div id="2tF5r5mecT" class="modal fade show" tabindex="-1" role="dialog" aria-labelledby="2tF5r5mecTLabel" data-backdrop="static" data-keyboard="false" aria-modal="true" style="display: block;">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="2tF5r5mecTLabel">System Message</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">Are you sure you want to create this item?</div>
                <div class="modal-footer">
                <button class="btn btn-primary" data-dismiss="modal">Yes</button><button class="btn btn-secondary" data-dismiss="modal">No</button></div>
            </div>
        </div>
    </div>
    <div class="modal-backdrop fade show"></div>
    

    enter image description here

    通过随机测试,我成功地推断出 fade 类在生成模式时允许关闭它而不进行任何其他更改。

    从

    jQuery("<div/>", {
            id: id,
            class: "modal fade",
            // href: 'http://google.com',
            //title: title,
            //rel: 'external',
            //text: message
        })
    

    到

    jQuery("<div/>", {
            id: id,
            class: "modal",
            // href: 'http://google.com',
            //title: title,
            //rel: 'external',
            //text: message
        })
    

    我的代码可以工作,但没有衰减效果。我已经检查了所有自定义的CSS .fade 但我没有。当问题持续存在时,浏览器上没有控制台错误。你们中有人遇到过这个问题吗?我刚升级到jquery 3.3.1和bootstrap 4.2.1。当没有褪色效果的时候感觉很奇怪。

    柱塞

    https://next.plnkr.co/edit/71QNigcwmUombEQ8?open=lib%2Fscript.js&preview

    1 回复  |  直到 7 年前
        1
  •  2
  •   Louis    7 年前

    jQuery .load() 是异步的。 问题是当 .modal("show") 执行时,模式的内容尚未加载。 如果修改代码以调用 模态(“显示”) 在你传递给的回调中 加载() 然后它工作。

    我已经 forked 你的劫掠者,所以你可以测试它。我更改了创建对话元素和调用的代码 模态(“显示”) 对此:

    jQuery("<div/>", {
        id: id,
        class: "modal fade",
        // href: 'http://google.com',
        //title: title,
        //rel: 'external',
        //text: message
    })
        .attr("tabindex", "-1")
        .attr("role", "dialog")
        .attr("aria-labelledby", id + "Label")
        .attr("aria-hidden", true)
        .attr("data-backdrop", "static")
        .attr("data-keyboard", false)
        .load("BootstrapDialogTemplate.html", function () {
            const $this = $(this);
            $this.find(".modal-title")
                .attr("id", id + "Label")
                .text(dialogTitle);
            $this.find(".modal-body", this).text(dialogMessage);
    
            var footer = $this.find(".modal-footer", this);
    
            dialogButtons.forEach(function (element) {
                $('<button/>', {
                    text: element.Text,
                    click: function () {
                        if (element.Event) {
                            element.Event();
                        }
                    },
                    class: element.Class
                })
                    .attr("data-dismiss", "modal")
                    .appendTo(footer);
            });
            $this.appendTo(document.body)
                .modal("show");
        });
    

    主要变化是:

    1. 移动 .appendTo(document.body).modal("show"); 在回调中。

    2. 添加 const $this = $(this) 然后使用 $this.find() 获取要修改的模板片段。

    在这一点上,读者会问“好的,但是等一下……为什么在手术不使用的时候它能工作? fade ?“

    这很复杂。基本事实是 通常地 引导程序设计不适用于部分组件。当调用一个函数,告诉引导程序使用dom元素作为引导程序组件时, 引导程序关心的所有部分都需要存在 .注意那些引导程序不关心的部分并不重要。您可以将一段文本异步加载到模式的主体中,或者加载到一个图像中,引导程序就可以很好地处理这个问题。它不关心这些事情。但当你加载 div 那需要 class="modal-dialog" 异步运行时会遇到问题,因为这是引导程序用来为组件提供行为的DOM结构的一部分。

    在原始代码中,创建模式时, this._dialog 领域 Modal 对象获取值 null 因为在DOM树中还没有匹配的元素。事实上,当你不使用它的时候,它似乎起作用 褪色 是偶然的。当你使用 褪色 代码路径更多地利用 这个对话框 因此更受 这个对话框 被设置为 无效的 . 当你不使用 褪色 , 这个对话框 正好少用了一点 看 好像工作得很好,但那是运气。我追踪了执行路径 褪色 看到一些奇怪的事情发生了。我希望在路上遇到麻烦。

    最终,您希望您的对话组件 全部的 在呼叫前显示在DOM中 模态(“显示”) .