您可以使用 JavaScript 创建“模态”元素。优点是您可以重复使用它。我已经使用 JavaScript 创建了一个示例 Bootstrap 模态框。您可以修改它以添加更多功能。
var Interface = {};
Interface.component = function ( dom ) {
this.dom = dom;
};
Interface.component.prototype = {
setId: function ( id ) {
this.dom.id = id;
return this;
}
};
Interface.BootstrapModal = function ( id, modalHeading, modalBodyContents, successButtonText, failureButtonText ) {
var scope = this;
var dom = document.createElement( 'div' );
dom.className = 'modal fade';
dom.id = id;
dom.role = "dialog";
var modalDialog = document.createElement( 'div' );
modalDialog.className = 'modal-dialog';
var modalContent = document.createElement( 'div' );
modalContent.className = 'modal-content';
var modalHeader = document.createElement( 'div' );
modalHeader.className = 'modal-header';
var modalHeaderXButton = document.createElement( 'button' );
modalHeaderXButton.className = 'close';
modalHeaderXButton.setAttribute("data-dismiss", "modal");
modalHeaderXButton.innerHTML = '×';
var modalHeaderHeading = document.createElement( 'h3' );
modalHeaderHeading.className = 'modal-title';
modalHeaderHeading.innerHTML = modalHeading;
modalHeader.appendChild(modalHeaderXButton);
modalHeader.appendChild(modalHeaderHeading);
var modalBody = document.createElement( 'div' );
modalBody.className = 'modal-body';
modalBody.appendChild(modalBodyContents);
var modalFooter = document.createElement( 'div' );
modalFooter.className = 'modal-footer';
var modalFooterSuccessButton = document.createElement( 'button' );
modalFooterSuccessButton.className = 'btn btn-success';
modalFooterSuccessButton.id = "<GIVE THE ID YOU NEED>";
modalFooterSuccessButton.innerHTML = successButtonText;
var modalFooterFailureButton = document.createElement( 'button' );
modalFooterFailureButton.className = 'btn btn-danger';
modalFooterFailureButton.id = "<GIVE THE ID YOU NEED>";
modalFooterFailureButton.setAttribute("data-dismiss", "modal");
modalFooterFailureButton.innerHTML = failureButtonText;
modalFooter.appendChild(modalFooterSuccessButton);
modalFooter.appendChild(modalFooterFailureButton);
modalContent.appendChild(modalHeader);
modalContent.appendChild(modalBody);
modalContent.appendChild(modalFooter);
modalDialog.appendChild(modalContent);
dom.appendChild(modalDialog);
modalFooterSuccessButton.addEventListener( 'click', function ( event ) {
} );
this.dom = dom;
return this;
};
Interface.BootstrapModal.prototype = Object.create( Interface.component.prototype );
Interface.BootstrapModal.prototype.constructor = Interface.BootstrapModal;
Interface.BootstrapModal.prototype.show = function () {
$('#' + this.dom.id).modal({backdrop: 'static', keyboard : false});
};
Interface.BootstrapModal.prototype.hide = function () {
$('#' + this.dom.id).modal('hide');
};
我创建了一个对象作为模态框元素。你可以像这样使用它:
var modalContent = document.createElement( 'div' );
var m = new Interface.BootstrapModal( 'id', 'modalHeading', modalContent, 'successButtonText', 'cancelButtonText' );
document.getElementById( <SOME ELEMENT> ).appendChild( m.dom );
现在m.show()将显示模态框,m.hide()将隐藏相同的模态框。
这可以用作Bootstrap模态框的通用模板。