Bootstrap指标不会在点击时起作用

时间:2017-12-08 08:15:12

标签: javascript jquery twitter-bootstrap carousel

当幻灯片更改指示器处于正确位置时(1-2-3)。但是当我点击它们时,它们没有响应。上一个和下一个链接工作正常。这是我的代码:

<div id="theCarousel" class="carousel-slide hidden-xs" data-interval ="3000">
        <!-- Indicators -->
        <ol class="carousel-indicators">
            <li data-target="#theCarousel" data-slide-to="0" class="active"></li>
            <li data-target="#theCarousel" data-slide-to="1"></li>
            <li data-target="#theCarousel" data-slide-to="2"></li>
        </ol>
        <div class="carousel-inner">
            <div class="item active"><img class="img-responsive" src="eberhard-grossgasteiger-311213.jpg" alt="slide 1"></div>
            <div class="item"><img class="d-block img-fluid" src="annie-spratt-176376.jpg" alt="slide 2"></div>
            <div class="item"><img class="d-block img-fluid" src="joanna-kosinska-129039.jpg" alt="slide 3"></div>
        </div><!-- /carousel-inner -->
        <!-- navigation -->
        <a class="left carousel-control" href="#theCarousel" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a>
        <a class="right carousel-control" href="#theCarousel" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>
    </div><!-- /carousel-slide --

这是JS文件:

// Activate Carouse
$("#theCarousel").carousel();

// Enable Carousel Indicators
$(".item").click(function(){
    $("#theCarousel").carousel(1);
 });

// Enable Carousel Control
 $(".left").click(function(){
     $("#theCarousel").carousel("prev");
 });

 $(".right").click(function(){
     $("#theCarousel").carousel("next");
 });

有什么问题?

2 个答案:

答案 0 :(得分:2)

单击“运行代码片段”按钮并检查其工作情况,无需添加额外脚本(js),由引导程序处理的轮播的所有控件。

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Carousel Example</h2>  
  <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>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner">
      <div class="item active">
        <img src="http://midtowncarpetcleaning.com/wp-content/themes/envision/lib/images/default-placeholder-1000x600.png" alt="Los Angeles" style="width:100%;">
      </div>

      <div class="item">
        <img src="http://midtowncarpetcleaning.com/wp-content/themes/envision/lib/images/default-placeholder-1000x600.png" alt="Chicago" style="width:100%;">
      </div>
    
      <div class="item">
        <img src="http://midtowncarpetcleaning.com/wp-content/themes/envision/lib/images/default-placeholder-1000x600.png" alt="New york" style="width:100%;">
      </div>
    </div>

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

</body>
</html>

答案 1 :(得分:1)

我终于发现了什么问题。而不是两个类:<div id="myCarousel" class="carousel slide" data-ride="carousel">我用连字符写了一个:<div id="theCarousel" class="carousel-slide hidden-xs" data-interval ="3000">