我可以在一个函数中初始化引导旋转木马吗?或者只能在ready函数中完成

Can I initialize bootstrap carousel in a function? or it should be done only in ready function?

本文关键字:函数 或者 ready 旋转木马 一个 我可以 初始化      更新时间:2023-09-26
  $('.carousel').carousel({
        pause: true,
        interval: false
    });

我想在函数中初始化它,但它不起作用

有什么办法可以做到吗?

供参考,我试着把它放在document.ready函数

请记住,您必须加载jquery.js(库)文件

,然后加载bootstrap jquery文件,然后自定义js文件。

然后像这样调用carousel函数

$('.carousel').carousel() // here carousel is the class of a div

添加如下选项

$( document ).ready(function(){
    $('.carousel').carousel({
      interval: 2000
    })
});

如果要在html页面中包含此脚本,请注意在html页面末尾附近包含此函数调用。

可以在函数中的任何位置初始化函数。但是当你再次调用相同的函数时它会被重新初始化。因此,请记住重新初始化功能。

相关文章: