Javascript 动态物化模态
我想在materialize中动态创建模态。 这是我的javascript代码:Javascript 动态物化模态,javascript,jquery,html,materialize,Javascript,Jquery,Html,Materialize,我想在materialize中动态创建模态。 这是我的javascript代码: $(document).ready(function(){ var modalContainer = $('<div id="modal_id" class="modal"></div>'); var modalContent = $('<div class="modal-content"><p>my content</p
$(document).ready(function(){
var modalContainer = $('<div id="modal_id" class="modal"></div>');
var modalContent = $('<div class="modal-content"><p>my content</p></div>');
modalContainer.append(modalContent);
modalContainer.modal(); // or modalContainer.modal('open');
});
$(文档).ready(函数(){
var modalContainer=$('');
var modalContent=$(“mycontent”);
modalContainer.append(modalContent);
modalContainer.modal();//或modalContainer.modal('open');
});
这是我的触发按钮:
<a class="waves-effect waves-light btn modal-trigger" href="#modal_id">Show Modal</a>
但是,这是行不通的。当我点击显示模态时,显示模态的包装,不显示模态框。
请帮帮我。谢谢。您可以用这种简单的方法来完成。创建一个空的
div
,该div带有.modal
类和任何要分配的id
然后在jQuery中,创建一个变量,并使用class模式内容保存一个div
。在该div
中,指定要动态添加的消息或内容。之后,使用$(.modal).append()
将该变量附加到modal,并使用.modal()
打开您的modal
请看示例
我已经创建了一个jsfiddle
看看它
演示-
实例
HTML
<a class="waves-effect waves-light btn modal-trigger" href="#modal1">Modal</a>
<div id="modal1" class="modal"></div>
jQuery
$(document).ready(function(){
// Here specify your content or message, enclose between <p>
var content = '<div class="modal-content"><p>my content</p></div>';
$('.modal').append(content);
$('.modal').modal();
});
$(文档).ready(函数(){
//在此处指定您的内容或消息,并在
var content='my content';
$('.modal')。追加(内容);
$('.modal').modal();
});
您可以用这种简单的方法来完成。创建一个空的div
,该div带有.modal
类和任何要分配的id
然后在jQuery中,创建一个变量,并使用class模式内容保存一个div
。在该div
中,指定要动态添加的消息或内容。之后,使用$(.modal).append()
将该变量附加到modal,并使用.modal()
打开您的modal
请看示例
我已经创建了一个jsfiddle
看看它
演示-
实例
HTML
<a class="waves-effect waves-light btn modal-trigger" href="#modal1">Modal</a>
<div id="modal1" class="modal"></div>
jQuery
$(document).ready(function(){
// Here specify your content or message, enclose between <p>
var content = '<div class="modal-content"><p>my content</p></div>';
$('.modal').append(content);
$('.modal').modal();
});
$(文档).ready(函数(){
//在此处指定您的内容或消息,并在
var content='my content';
$('.modal')。追加(内容);
$('.modal').modal();
});
我编写了一个小类来动态创建模态。注意,设置高度和宽度有点麻烦,我真的不知道为什么。在我的应用程序中,我只需要固定的页脚模式,但是可以轻松地动态设置该类以满足您的需要。不管怎样,我希望它能帮助:
class Modal {
constructor(opt) {
this.name = opt.name;
this.width = opt.width || '';
this.height = opt.height || '';
this.topMargin = opt.height ? Math.floor((100 - this.height) / 2) : '';
this.style = (opt.width && opt.height) ? 'width: ' + this.width + '%; height: ' + this.height + 'vh; top: ' + this.topMargin + 'vh;' : opt.width ? 'width: ' + this.width + '%;' : opt.height ? 'height: ' + this.height + 'vh; top: ' + this.topMargin + 'vh' : '';
this.footerButtons = opt.footerButtons || '';
this.openButton = opt.openButton || null;
this.title = opt.title || '';
this.onOpen = opt.onOpen;
this.fixedContent = opt.fixedContent || '';
this.template = '<div id="' + this.name + '" class="modal modal-fixed-footer" style="' + this.style + '"><div class="modal-content" style="padding-top: 17px;"><h4>' + this.title + '</h4><div class="divider"></div><br>' + this.fixedContent + '<div class="modal-content-dynamic"></div></div><div class="modal-footer">' + this.footerButtons + '</div></div>';
$('.container').append(this.template);
var self = this;
if (this.openButton) {
$(document).on('click', this.openButton, function () {
self.open();
});
}
}
open() {
$('#' + this.name).openModal({
dismissable: false,
preventScrolling: false
});
if (this.onOpen) this.onOpen(this);
}
close() {
$('#' + this.name).closeModal();
}
setContent(content) {
$('#' + this.name).find('.modal-content-dynamic').html(content);
}
addContent(content) {
$('#' + this.name).find('.modal-content-dynamic').append(content);
}
setTitle(title) {
$('#' + this.name).find('.modal-content h4').html(title);
}
setFooterButtons(buttons) {
$('#' + this.name).find('.modal-footer').html(buttons);
}
}
类模态{
建造商(opt){
this.name=opt.name;
this.width=opt.width | |“”;
this.height=opt.height | |“”;
this.topMargin=opt.height?数学地板((100-this.height)/2):“”;
this.style=(opt.width&&opt.height);“width:”+this.width+”;height:“+this.height+”;top:“+this.topMargin+”vh;”:opt.width?“;width:”+this.width+”;height:“+this.height+”vh;top:“+this.topMargin+”vh:”;
this.footerButtons=opt.footerButtons | |“”;
this.openButton=opt.openButton | | null;
this.title=opt.title | |“”;
this.onOpen=opt.onOpen;
this.fixedContent=opt.fixedContent | |“”;
this.template=''+this.title+'
'+this.fixedContent+''+this.footerButtons+'';
$('.container').append(this.template);
var self=这个;
如果(此.openButton){
$(文档)。在('click',this.openButton,函数(){
self.open();
});
}
}
开(){
$('#'+this.name).openmodel({
可驳回:假,
防止滚动:false
});
如果(this.onOpen)this.onOpen(this);
}
关闭(){
$('#'+this.name).closeModal();
}
设置内容(内容){
$('#'+this.name).find('.modal content dynamic').html(content);
}
添加内容(内容){
$('#'+this.name).find('.modal content dynamic').append(content);
}
标题(标题){
$('#'+this.name).find('.modal content h4').html(title);
}
设置页脚按钮(按钮){
$('#'+this.name).find('.modal footer').html(按钮);
}
}
然后像这样使用它:
var testModal = new Modal({
name: 'testModal',
openButton: '#open_testModal',
title: '<b>TestModal</b>',
fixedContent: '<p>Some static content</p>',
onOpen: function (modal) {
// do something every time the modal is opened
}
});
var testModal=新模态({
名称:“testModal”,
openButton:“#打开测试模式”,
标题:“TestModal”,
fixedContent:“一些静态内容”,
onOpen:功能(模态){
//每次打开模式时都要执行一些操作
}
});
我编写了一个小类来动态创建模态。注意,设置高度和宽度有点麻烦,我真的不知道为什么。在我的应用程序中,我只需要固定的页脚模式,但是可以轻松地动态设置该类以满足您的需要。不管怎样,我希望它能帮助:
class Modal {
constructor(opt) {
this.name = opt.name;
this.width = opt.width || '';
this.height = opt.height || '';
this.topMargin = opt.height ? Math.floor((100 - this.height) / 2) : '';
this.style = (opt.width && opt.height) ? 'width: ' + this.width + '%; height: ' + this.height + 'vh; top: ' + this.topMargin + 'vh;' : opt.width ? 'width: ' + this.width + '%;' : opt.height ? 'height: ' + this.height + 'vh; top: ' + this.topMargin + 'vh' : '';
this.footerButtons = opt.footerButtons || '';
this.openButton = opt.openButton || null;
this.title = opt.title || '';
this.onOpen = opt.onOpen;
this.fixedContent = opt.fixedContent || '';
this.template = '<div id="' + this.name + '" class="modal modal-fixed-footer" style="' + this.style + '"><div class="modal-content" style="padding-top: 17px;"><h4>' + this.title + '</h4><div class="divider"></div><br>' + this.fixedContent + '<div class="modal-content-dynamic"></div></div><div class="modal-footer">' + this.footerButtons + '</div></div>';
$('.container').append(this.template);
var self = this;
if (this.openButton) {
$(document).on('click', this.openButton, function () {
self.open();
});
}
}
open() {
$('#' + this.name).openModal({
dismissable: false,
preventScrolling: false
});
if (this.onOpen) this.onOpen(this);
}
close() {
$('#' + this.name).closeModal();
}
setContent(content) {
$('#' + this.name).find('.modal-content-dynamic').html(content);
}
addContent(content) {
$('#' + this.name).find('.modal-content-dynamic').append(content);
}
setTitle(title) {
$('#' + this.name).find('.modal-content h4').html(title);
}
setFooterButtons(buttons) {
$('#' + this.name).find('.modal-footer').html(buttons);
}
}
类模态{
建造商(opt){
this.name=opt.name;
this.width=opt.width | |“”;
this.height=opt.height | |“”;
this.topMargin=opt.height?数学地板((100-this.height)/2):“”;
this.style=(opt.width&&opt.height);“width:”+this.width+”;height:“+this.height+”;top:“+this.topMargin+”vh;”:opt.width?“;width:”+this.width+”;height:“+this.height+”vh;top:“+this.topMargin+”vh:”;
this.footerButtons=opt.footerButtons | |“”;
this.openButton=opt.openButton | | null;
this.title=opt.title | |“”;
this.onOpen=opt.onOpen;
this.fixedContent=opt.fixedContent | |“”;
this.template=''+this.title+'
'+this.fixedContent+''+this.footerButtons+'';
$('.container').append(this.template);
var self=这个;
如果(此.openButton){
$(文档)。在('click',this.openButton,函数(){