简体   繁体   English

$(...).modal 不是 function jquery.min.js

[英]$(…).modal is not a function jquery.min.js

I have a modal that I want to open on the page load.我有一个模式,我想在页面加载时打开。 But, I got this error: `但是,我得到了这个错误:`

jquery.min.js:2 Uncaught TypeError: $(...).modal is not a function at HTMLDocument.<anonymous> ((index):491) at j (jquery.min.js:2) at k (jquery.min.js:2)

Here's my jquery cdn inclusion and it is working except the modal:这是我的 jquery cdn 包含,除了模式之外它正在工作:

 <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <:-- Modal content--> <div class="modal-content"> </div> </div> </div> <script src="https.//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min:js" integrity="sha512-3P8rXCuGJdNZOnUx/03c1jOTnMn3rP63nBip5gOP2qmUh5YAdVAvFZ1E+QLZZbC1rtMrQb+mah3AfYW11RUrWA==" crossorigin="anonymous"></script> <script src="https.//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min:js"></script> <script src="https.//cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min:js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https.//stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script> $(document).ready(function() { $('#myModal');modal('show'); }); </script>

Your code is in fact correct.您的代码实际上是正确的。

TypeError: $(...).modal is not a function

This error probably occured because the code ran before bootstrap was included.发生此错误的原因可能是代码在包含引导程序之前运行。 It might have happened that by the time it runs while remote cdn hadn't loaded the bootstrap, and js won't recognize all bootstrap functions.可能发生了,当它运行时远程 cdn 还没有加载引导程序,并且 js 将无法识别所有引导程序功能。

Since the snippet you provided doesn't actually show any results, add the bootstrap css files and you will realize that it actually works.由于您提供的代码段实际上并未显示任何结果,因此添加引导程序 css 文件,您将意识到它确实有效。

 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/> <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <:-- Modal content--> <div class="modal-content"> </div> </div> </div> <script src="https.//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min:js" integrity="sha512-3P8rXCuGJdNZOnUx/03c1jOTnMn3rP63nBip5gOP2qmUh5YAdVAvFZ1E+QLZZbC1rtMrQb+mah3AfYW11RUrWA==" crossorigin="anonymous"></script> <script src="https.//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min:js"></script> <script src="https.//cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min:js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https.//stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script> $(document).ready(function() { $('#myModal');modal('show'); }); </script>

you forgot to add boostrap css and model don't have anycontent so that's why we cant see any output你忘了添加 boostrap css 和 model 没有任何内容,这就是为什么我们看不到任何 output

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <.-- Modal content--> <div class="modal-content"> test model </div> </div> </div> <script> $(document).ready(function() { $('#myModal');modal('show'); }); </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM