gpt4 book ai didi

javascript - 如何触发 Bootstrap Carousel 的 carousel-indicators

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:14:05 25 4
gpt4 key购买 nike

我的代码有什么问题?我想触发当前的 Bootstrap 轮播幻灯片编号。我想在特定幻灯片时更改文本或执行一些 jQuery 命令。请查看我的代码。

$(document).ready(function() {

$('#myCarousel').on('slide.bs.carousel', function (ev) {
var id = ev.relatedTarget.id;
switch (id) {
case "1":
$('#sometext').text("one");
break;
case "2":
$('#sometext').text("two");
break;
case "3":
$('#sometext').text("three");
default:
//the id is none of the above
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>


<h1 id="sometext">Some Text here</h1>


<div id="myCarousel" class="carousel slide" data-ride="carousel">


<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
<li data-target="#myCarousel" data-slide-to="3"></li>
</ol>

<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="http://www.w3schools.com/bootstrap/img_chania.jpg" alt="Chania" width="460" height="345">
</div>

<div class="item">
<img src="http://www.w3schools.com/bootstrap/img_chania2.jpg" alt="Chania" width="460" height="345">
</div>

<div class="item">
<img src="http://www.w3schools.com/bootstrap/img_flower.jpg" alt="Flower" width="460" height="345">
</div>

<div class="item">
<img src="http://www.w3schools.com/bootstrap/img_flower2.jpg" alt="Flower" width="460" height="345">
</div>
</div>

<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>

最佳答案

尝试

$(document).ready(function() {    
$('#myCarousel').on('slide.bs.carousel', function (ev) {
var idx = $(ev.relatedTarget).index() + 1;
console.log(idx);
});
})

关于javascript - 如何触发 Bootstrap Carousel 的 carousel-indicators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37044805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com