我正在尝试使用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');
}
答案 0 :(得分:0)
此行失败:header.appendChild(header);
因为它无法将标头附加到自身。你是说这个吗?
var title = document.createElement('h4');
title.className = 'modal-title';
title.createTextNode = 'Data Error';
header.appendChild(title);
调试工具在javascript开发中非常有用(例如Firefox的Firebug插件或Chrome中的内置工具)。您可能会立即看到此错误:)