【问题标题】:MaterializeCss modal Error openModal is not a functionMaterializeCss modal 错误 openModal is not a function
【发布时间】:2017-02-26 06:15:22
【问题描述】:

我有所有要求 Jquery,Materialize.js 位于我的 Js 文件上方,但是我收到警告 openModal is not a function..我检查了模式名称是否正确,我可以运行 Materialize.toast,所以我知道 Materialize.js 是在职的。使用按钮触发也不会调用模态。这是代码..

脚本:

<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="/js/materialize.js"></script>
<script type="text/javascript" src="/js/video.js"></script>
<script src="/js/admin.js"></script>

触发器:

<button data-target="modal1" class="btn modal-trigger">Modal</button>

模态:

    <!-- Modal Structure -->
    <div id="modal1" class="modal">
        <div class="modal-content">
            <h4>Modal Header</h4>
            <p>A bunch of text</p>
        </div>
        <div class="modal-footer">
            <a href="#!" class=" modal-action modal-close waves-effect waves-green btn-flat">Agree</a>
        </div>
    </div>
</div>

js:

 var modal = document.getElementById('modal1');
  modal.openModal();
$('#modal1').leanModal();
$('#modal1').openModal();

【问题讨论】:

    标签: javascript jquery material-design materialize


    【解决方案1】:

    将压缩文件导入materialize/dist文件夹而不是materialize/js。

    运行: $('#elementId').modal(); $('#elementId').modal('show');

    就这样成功了。

    编辑:

    事实上,它在生产环境中不起作用。

    所以我这样做了:

    (function( $ ) {
    
        $.fn.showModal = function() {
    
            var self = this;
            try {
                this.modal();
                self.modal('open');
            } catch (err) {
                try{
                    self.openModal();
                } catch (err2) {
                    console.error("Something went wrong - cannot open modal.");             
                }
            }
        }
    
    
        $.fn.hideModal = function() {
    
            var self = this;
            try {
                this.modal();
                self.modal('close');
            } catch (err) {
                try{
                    self.closeModal();
                } catch (err2) {
                    console.error("Something went wrong - cannot close modal.");                
                }
            }
        }
    
    
    }( jQuery ));
    

    只使用:$('#modal').showModal()

    【讨论】:

      【解决方案2】:

      好的,我遇到了同样的问题,我通过更改打开模式和初始化的调用来修复它。

      如果您遇到与我相同的问题,请查看最新文档 here

      这可能是新的——这里没有使用 openModal()。

      初始化:

      <script>
         try {
             try {
            $('#modalId').modal();
        } catch (e) {
            console.info('loading modal auto-initialized..');
        }
         }
      </script>
      

      我添加了 try/catch,因为有时不需要这样做

      实施:

      $('#modalID').modal('open');
      

      【讨论】:

        【解决方案3】:

        实现函数需要 Jquery Elements。
        getElementById() - 给我们 DOM 对象。

        //You can either convert this Dom object to Jquery
        
        var modal = document.getElementById('modal1');
        var jquerymodal = $(modal);  //convert to jQuery Element
        jquerymodal.openModal();
        
        
        //Or just use Jquery Element like
        
        $('#modal1').openModal();
        

        【讨论】:

        • 我试图给它 Jquery 也没有接受。响应:Uncaught TypeError: $(...).openModal is not a function
        猜你喜欢
        • 2014-09-18
        • 2023-02-20
        • 2017-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多