Bootstrap Carousel中的项数返回0



我正在尝试获取引导转盘中项目的长度(总数(。然而,我的代码不断返回0:

var totalItems = $('.carousel-item').length;

数据骑乘在父div中声明:

<div id="carouselExampleIndicators" class="carousel slide carousel-fade my-4 " data-ride="carousel">

当我尝试手动调用旋转木马时:

$('.carousel').carousel()

我在控制台中得到一个异常:

$('.carousel'(.cab((不是函数;是未定义的

不确定这两个问题是否相关。

转盘项目标记为转盘项目类别:

<div class="carousel-item">
<img class="d-block w-100 img-fluid imageCarousel"  src="images/fullpics/colesbay.jpg" alt="Image 2">
<div class="carousel-caption d-none d-md-block text-center">
<h5>Second slide label</h5>
<p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
</div>
</div>

我在网上检查的所有例子都使用这种方法来计算项目的数量,所以不确定我哪里出了问题。我也检查过这个答案,但没有找到解决方案。

完整代码:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<!--V4 does not support glyphicons so use this CDN and pre-append fa fa to all glphicon glyphicon calls-->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<!--Bootstrap JQuery and Popper.js and custom Bootstap JS-->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

引导转盘:

<!--Main body - Botstrap Carousle-->
<div class="col-lg-9">
<div id="carouselExampleIndicators" class="carousel slide carousel-fade my-4 " data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0"></li>
<!--
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>-->
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<!--Set the height of  div class to set height of all images as externa css imageCarsoule to stop jumping-->
<div  class="carousel-inner" style="height: 400;"  role = "listbox">
<!--Remove class="carousel-item-active so we can randomly assign image with carousel.js
<div class="carousel-item-active">-->
<div class="carousel-item">
<img class="d-block w-100 img-fluid imageCarousel" src="images/fullpics/landscapes/Old Sheds Evandale (Acrylic).tiff" alt="First Mage">
</div>
<div class="carousel-item">
<img class="d-block w-100 img-fluid imageCarousel"  src="images/fullpics/colesbay.jpg" alt="Image 2">
<div class="carousel-caption d-none d-md-block text-center">
<h5>Second slide label</h5>
<p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
</div>
</div>
<div class="carousel-item">
<img class="d-block w-100 img-fluid imageCarousel" src="images/fullpics/landscapes/Sheoaks at Coles Bay (Acrylic & Coloured Pencil).tiff" alt="Image 2">
<div class="carousel-caption d-none d-md-block text-center">
<h5>third slide label</h5>
<p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
</div>
</div>
<div class="carousel-item">
<img class="d-block w-100 img-fluid imageCarousel"  src="images/fullpics/landscapes/Winter Drovers (Egg Tempera).jpg" alt="Image 2">
<div class="carousel-caption d-none d-md-block text-center">
<h5>Fourth slide label</h5>
<p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
</div>
</div>
<div class="carousel-item">
<img class="d-block w-100 img-fluid imageCarousel "src="images/fullpics//landscapes/Early Start (Oil).tiff" alt="Image 2">
<div class="carousel-caption d-none d-md-block text-center">
<h5>Fifth / Last slide label</h5>
<p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
</div>
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>

我的外部JS文件:

$(document).ready(function() {
$("#ccarouselExampleIndicators").carousel({
interval: 1000,
ride:true
});
console.log($(".carousel-item").length);
});

也许它可以成为库的主题,从官方引导页面中获取库

$(document).ready(function() {
$("#carouselExampleSlidesOnly").carousel({
interval: 1000,
ride:true
});
console.log($(".carousel-item").length);
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<div id="carouselExampleSlidesOnly" class="carousel slide">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="..." alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="Third slide">
</div>
</div>
</div>

在您的情况下,carousel函数需要由id调用。转盘id是carouselExampleIndicators,所以您需要通过调用它

$('#carouselExampleIndicators').carousel()

下面是一个使用模板的示例。你还放了style="height: 400;",它不是一个有效的css规则

style="height: 400px;"(注意:400px不会显示标签(

最后没有第一张幻灯片被标记为活动,因此转盘不会启动。使用

<div class="carousel-item active">

在一张幻灯片上

相关内容

  • 没有找到相关文章

最新更新