简体   繁体   English

延迟后无法加载Bootstrap模态

[英]Loading Bootstrap modal after delay not working

I'm working on a cratejoy site, and my client needs a modal to popup after a set amount of time. 我正在一个cratejoy网站上工作,我的客户需要一个经过一定时间后才能弹出的模式。 I added the modal and when I use this it works fine: 我添加了模态,当我使用它时,它运行良好:

$('#myModal').modal('show');

But when I try to use either of these answers from this page , it get a Uncaught TypeError: $(...).modal is not a function error and the modal does not popup. 但是,当我尝试使用此页面上的任何一个答案时,都会收到Uncaught TypeError:$(...)。modal不是函数错误,并且不会弹出模式。 You can see it not working here . 您可以看到它在这里不起作用。

You have multiple versions of jQuery being loaded 您正在加载多个版本的jQuery

Apart from the obvious version with it's own script tag your main.js file also includes it. 除了带有自己的脚本标记的明显版本之外,您的main.js文件还包括它。

Since it loads after bootstrap.js it overwrites the whole jQuery object and removes reference to bootstrap in original version. 由于它是在bootstrap.js之后加载的,因此会覆盖整个jQuery对象,并删除原始版本中对bootstrap的引用。 That is why you get the error shown 这就是为什么您显示错误

Choose which version of jQuery you want to use and remove the other. 选择您要使用的jQuery版本,然后删除另一个。

Make sure boostrap.js loads after the selected choice 确保在选择后加载boostrap.js

First of all you need to have link to bootstrap.js in your page and loaded after jquery. 首先,您需要在页面中链接到bootstrap.js并在jquery之后加载。

Second you have to place the script in a script tag before the end body tag 其次,您必须将脚本放置在end body标签之前的script标签中

$(document).ready(function(){
   $('#myModal').modal('show');
}); 

In order to delay the modal you can do this: 为了延迟模态,您可以这样做:

setTimeout(function(){
    $('#myModal').modal('show');
}, 5000)

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

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