简体   繁体   English

Boostrap Modal示例未显示

[英]Boostrap Modal example doesn't show

I'm testing Bootstrap Modal example " live demo " and I've included bootstrap jquery and js in bootstrap starter template , and also the custom javascript: 我正在测试Bootstrap Modal示例“ live demo ”,并且在bootstrap starter模板中包含了bootstrap jquery和js,并且还自定义了javascript:

$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').focus()
})

but the modal run from my text editor doesn't show like the one on their website does. 但是我的文本编辑器显示的模式运行结果却不像他们网站上的模式显示那样。 I click the button and nothing happens. 我单击按钮,没有任何反应。 I've tried using Google Chrome Inspector/console and it shows: 我尝试使用Google Chrome浏览器检查器/控制台,它显示:

Uncaught ReferenceError: $ is not defined 未捕获的ReferenceError:未定义$

at the line where the function starts. 在函数开始的那一行。 When I remove the custom js, it doesn't show error but still the modal doesn't show on Chrome, Edge or Firefox. 当我删除自定义js时,它不会显示错误,但仍然不会在Chrome,Edge或Firefox上显示模式。

My code is just the combination of Bootstrap starter template and its modal live demo code in the body so there's no need to post the code here. 我的代码只是Bootstrap入门模板及其主体中的模态实时演示代码的组合,因此无需在此处发布代码。

Also, please note that I could make the code work (the button works like the example shown on boostrap website) by assigning an id to the button: 另外,请注意,我可以通过为按钮分配ID来使代码正常工作(按钮的功能类似于boostrap网站上显示的示例):

 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" id="button">

then add a custom function later: 然后稍后添加自定义函数:

 <script type="text/javascript"> $("#button").click(function() { $('.modal').modal('show'); }); </script> 

However, that's not the same as the sample code provided by Bootstrap. 但是,这与Bootstrap提供的示例代码不同。

Generally, this error occurs because you're trying to use JQuery without having referenced it in your project first. 通常,发生此错误是因为您尝试使用JQuery而不先在项目中引用它。

Try adding the following to the top your page: 尝试在页面顶部添加以下内容:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

Thank you all for your suggestions. 谢谢大家的建议。 I've found out the answer: The sample code by bootstrap has an error: 我找到了答案:引导程序的示例代码有一个错误:

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
</button>

the data-target="" should contain the id of the modal, which, in this case is #myModal not #exampleModal. data-target=""应该包含模式的ID,在本例中为#myModal而不是#exampleModal。

it means you are running your script before document is ready. 这意味着您在文档准备就绪之前正在运行脚本。 Try this 尝试这个

$(function(){
   $('#myModal').on('shown.bs.modal', function () {
     $('#myInput').focus()
   })
});

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

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