【问题标题】:Materialize modal dynamically动态物化模态
【发布时间】:2018-04-09 03:55:24
【问题描述】:

我想在物化中动态创建模态。 这是我的 javascript 代码:

    $(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');
    });

这是我的触发按钮:

<a class="waves-effect waves-light btn modal-trigger" href="#modal_id">Show Modal</a>

但是,这不起作用。当我点击Show Modal 锚点时,显示模态的包装器并且不显示模态框。 请帮我。谢谢。

【问题讨论】:

  • This questions answer 可以帮到你。
  • @VTodorov 您可能要注意,链接的问题使用 Twitter Bootstrap 而不是 Materialize...

标签: javascript jquery html materialize


【解决方案1】:

您可以使用这种简单的方法来做到这一点。创建一个空的div.modal 类以及您要分配的任何id

然后在 jQuery 中,创建一个变量并保存一个 divmodal-content。在div 中指定您要动态添加的消息或内容。之后,使用$(.modal).append()将该变量附加到模态并使用.modal()打开你的模态

看看示例

我创建了一个jsfiddle 看看它。

演示 - 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();
});

【讨论】:

    【解决方案2】:

    您可以像这样使用纯 javascript 在 1.0.0 上打开模式

    const elem = document.getElementById('modalId here');
    const instance = M.Modal.init(elem, {dismissible: false});
    instance.open();
    

    【讨论】:

      【解决方案3】:

      我写了一个小类来动态创建模态。请注意,设置高度和宽度有点错误,我真的不知道为什么。我只需要在我的应用程序中使用固定页脚模式,但可以轻松地动态设置该类以满足您的需求。无论如何,我希望它有所帮助:

      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);
          }
      }
      

      然后像这样使用它:

      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
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-06
        • 2019-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多