HTML代码:
<div id="myCarousel" class="carousel slide"> <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> <div class="carousel-inner"> <div class="item active"> <img src="./images/lunbo1.jpg"> </div> <div class="item"> <img src="./images/lunbo2.jpg"> </div> <div class="item"> <img src="./images/lunbo3.jpg"> </div> <div class="item"> <img src="./images/lunbo4.jpg"> </div> </div> </div>
jQuery代码:
<!-- js引入 --> <script src="js/jquery-3.1.1.min.js"></script> <script src="js/bootstrap.min.js"></script> <script> $(function () { $('#myCarousel').carousel({interval: 4000}); //获取轮播的第一个,然后添加class样式 $(".carousel-inner").find("div").first().attr("class","item active"); $(".carousel-indicators").find("li").first().attr("class","active"); }); </script>