使用JavaScript创建引导模式对话框

Creating Bootstrap Modal Dialog using JavaScript

本文关键字:模式 对话框 JavaScript 创建 使用      更新时间:2023-09-26

我正在尝试使用JavaScript创建一个Bootstrap Modal对话框。我之所以使用JavaScript进行渲染,是因为我希望以后能够使用自定义元素(即标题、错误消息)进行渲染。我是JavaScript的新手,所以我不明白为什么当我调用函数errorMessage()时代码没有被执行。有人能帮我吗?让我知道错误是什么,以及我该如何纠正?有更有效的方法吗?非常感谢。

顺便说一句,我确实将modal.js文件链接到了HTML文档,我知道bootboxjs,但我现在不想使用它。

// Creation of the alert message box.
function errorMessage() {
    var Modal = document.createElement('div');
    Modal.id = 'myModal';
    Modal.className = 'modal fade show';
    // To set up the attributes.
    Modal.setAttribute("data-backdrop", "static");
    Modal.setAttribute("data-keyboard", false);
    document.body.appendChild(Modal);
        var dialog = document.createElement('div');
        dialog.className = 'modal-dialog';
        Modal.appendChild(dialog);
            var content = document.createElement('div');
            content.className = 'modal-content';
            dialog.appendChild(content);
                var header = document.createElement('div');
                header.className = 'modal-header';
                content.appendChild(header);
                    var title = document.createElement('h4');
                    title.className = 'modal-title';
                    title.createTextNode = 'Data Error';
                    header.appendChild(header);
                var body = document.createElement('div');
                body.className = 'modal-body';
                dialog.appendChild(body);
                    var message = document.createElement('p');
                    message.createTextNode("Oh... snap. We can't find any items in your list. Please make sure your entry is structured as following:");
                    body.appendChild(message);
                    var representation = document.createElement('div');
                    representation.className = 'well';
                    body.appendChild(representation);
                        var representationTxt = document.createElement('p');
                        representationTxt.style.fontStyle = 'italic';
                        representationTxt.createTextNode('> Subheader , tag1 , tag2, tag3');
                        representation.appendChild(representationTxt);
                var footer = document.createElement('div');
                footer.className = 'modal-footer';
                dialog.appendChild(footer);
                    var closeBtn = document.createElement('button');
                    closeBtn.setAttribute("data-dismiss", "modal");
                    closeBtn.setAttribute("type", "button");
                    closeBtn.className = 'btn btn-primary btn-inverse';
                    closeBtn.value = 'I got it. Thanks.';
                    footer.appendChild(closeBtn);
    // Show modal dialog box
    $("#myModal").modal('show');
}

它在以下行失败:header.appendChild(header);,因为它无法将头附加到自己。你是这个意思吗?

var title = document.createElement('h4');
title.className = 'modal-title';
title.createTextNode = 'Data Error';
header.appendChild(title);

调试工具在javascript开发中是非常宝贵的(例如,Firefox的Firebug插件或Chrome中的内置工具)。您会立即看到这个错误:)