Bootstrap 轮播:字幕不显示

Bootstrap Carousel: Captions not displaying

我想在 BS 轮播的图表上显示标题(h3/p 标签)。它没有显示,我已经推荐了 w3 学校并进行了一些更改,但根本不起作用。

下面是我试过的:

<div class="carousel-caption">
        <h3> Test caption1 </h3>
        <p> Test caption2 </p>
         </div>

完成html:

<!DOCTYPE html>
<html lang="en">
<head>
<style>
  .carousel-indicators li {
    background-color: #BFC9CA !important;
  }
  .carousel-indicators .active {
    background-color: #BFC9CA !important;
  }
  h3
  {
  background-color: gold !important;
  }
  .carousel-inner {
    width: 100%;
    max-height: 500px !important;
}
</style>
  <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.4.0/css/bootstrap.min.css" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
   <script type='text/javascript' src='https://www.gstatic.com/charts/loader.js'></script>
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type='text/javascript'>google.charts.load('current', { packages: ['corechart'] });</script>
     <script language='JavaScript'>function drawChart(PassCount, FailCount, DivID) { var data = google.visualization.arrayToDataTable([['Status', 'Outcome', { role: 'style' }], ['Passed', PassCount, '#8BC34A'], ['Failed', FailCount, '#ff4c4c']]); var groupData = google.visualization.data.group(data, [{ column: 0, modifier: function () { return 'total' }, type: 'string' }], [{ column: 1, aggregation: google.visualization.data.sum, type: 'number' }]); var formatPercent = new google.visualization.NumberFormat({ pattern: '#,##0.0%' }); var formatShort = new google.visualization.NumberFormat({ pattern: 'short' }); var view = new google.visualization.DataView(data); view.setColumns([0, 1, 2, { calc: function (dt, row) { var amount = formatShort.formatValue(dt.getValue(row, 1)); var percent = formatPercent.formatValue(dt.getValue(row, 1) / groupData.getValue(0, 1)); return amount + ' (' + percent + ')'; }, type: 'string', role: 'annotation' }]); var options = { 'legend': 'none', tooltip: { trigger: 'none' }, 'width': 650, 'height': 400, animation: { duration: 1500, startup: true } }; var chart = new google.visualization.ColumnChart(document.getElementById(DivID)); chart.draw(view, options); } google.charts.setOnLoadCallback(function () { drawChart(70, 5, 'GoogleColChart_0'); }); google.charts.setOnLoadCallback(function () { drawChart(80, 10, 'GoogleColChart_1'); }); google.charts.setOnLoadCallback(function () { drawChart(90, 15, 'GoogleColChart_2'); });</script>
    <link rel='icon' type='Icon.ico' href='Icon.ico' />
</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 align="center" class="item active" id='GoogleColChart_0' style='width:800; height:500;border-style:groove;border-width: 1px;'>
        <script type='text/javascript'>drawChart(70, 5, 'GoogleColChart_0');</script>
        <div class="carousel-caption">
        <h3> Test caption1 </h3>
        <p> Test caption2 </p>
         </div>
      </div>
      <div align="center" class="item" id='GoogleColChart_1' style='width:800; height:500;border-style:groove;border-width: 1px;'>
        <script type='text/javascript'>drawChart(80, 10, 'GoogleColChart_1');</script>
        <div class="carousel-caption">
        <h3> Test caption1 </h3>
        <p> Test caption2 </p>
         </div>
      </div>    
      <div align="center" class="item" id='GoogleColChart_2' style='width:800; height:500;border-style:groove;border-width: 1px;'>
            <script type='text/javascript'>drawChart(90, 15, 'GoogleColChart_2');</script>
    <div class="carousel-caption">
        <h3> Test caption1 </h3>
        <p> Test caption2 </p>
    </div>
      </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>

标题应显示在提到的位置上。

您的代码中的问题是您使用 class class="item active" 定位元素并且该元素正在被您的 drawChart 函数覆盖。

一个可能的解决方案是将 ID 移动到内部 div,如我的解决方案所示:

<!DOCTYPE html>
<html lang="en">
  <head>
    <style>
      .carousel-indicators li {
        background-color: #BFC9CA !important;
      }
      .carousel-indicators .active {
        background-color: #BFC9CA !important;
      }
      h3
      {
      background-color: gold !important;
      }
      .carousel-inner {
        width: 100%;
        max-height: 500px !important;
    }
    </style>
    <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.4.0/css/bootstrap.min.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
    <script type='text/javascript' src='https://www.gstatic.com/charts/loader.js'></script>
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type='text/javascript'>google.charts.load('current', { packages: ['corechart'] });</script>
    <script language='JavaScript'>function drawChart(PassCount, FailCount, DivID) { var data = google.visualization.arrayToDataTable([['Status', 'Outcome', { role: 'style' }], ['Passed', PassCount, '#8BC34A'], ['Failed', FailCount, '#ff4c4c']]); var groupData = google.visualization.data.group(data, [{ column: 0, modifier: function () { return 'total' }, type: 'string' }], [{ column: 1, aggregation: google.visualization.data.sum, type: 'number' }]); var formatPercent = new google.visualization.NumberFormat({ pattern: '#,##0.0%' }); var formatShort = new google.visualization.NumberFormat({ pattern: 'short' }); var view = new google.visualization.DataView(data); view.setColumns([0, 1, 2, { calc: function (dt, row) { var amount = formatShort.formatValue(dt.getValue(row, 1)); var percent = formatPercent.formatValue(dt.getValue(row, 1) / groupData.getValue(0, 1)); return amount + ' (' + percent + ')'; }, type: 'string', role: 'annotation' }]); var options = { 'legend': 'none', tooltip: { trigger: 'none' }, 'width': 650, 'height': 400, animation: { duration: 1500, startup: true } }; var chart = new google.visualization.ColumnChart(document.getElementById(DivID)); chart.draw(view, options); } google.charts.setOnLoadCallback(function () { drawChart(70, 5, 'GoogleColChart_0'); }); google.charts.setOnLoadCallback(function () { drawChart(80, 10, 'GoogleColChart_1'); }); google.charts.setOnLoadCallback(function () { drawChart(90, 15, 'GoogleColChart_2'); });</script>
    <link rel='icon' type='Icon.ico' href='Icon.ico' />
  </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 align="center" class="item active">
            <div id='GoogleColChart_0' style='width:800; height:500;border-style:groove;border-width: 1px;'>
              <script type='text/javascript'>drawChart(70, 5, 'GoogleColChart_0');</script>
            </div>
            <div class="carousel-caption">
              <h3> Test caption1 </h3>
              <p> Test caption2 </p>
            </div>
          </div>
          <div align="center" class="item">
            <div id='GoogleColChart_1' style='width:800; height:500;border-style:groove;border-width: 1px;'>
              <script type='text/javascript'>drawChart(80, 10, 'GoogleColChart_1');</script>
            </div>
            <div class="carousel-caption">
              <h3> Test caption1 </h3>
              <p> Test caption2 </p>
            </div>
          </div>
          <div align="center" class="item">
            <div id='GoogleColChart_2' style='width:800; height:500;border-style:groove;border-width: 1px;'>
              <script type='text/javascript'>drawChart(90, 15, 'GoogleColChart_2');</script>
            </div>
            <div class="carousel-caption">
              <h3> Test caption1 </h3>
              <p> Test caption2 </p>
            </div>
          </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>

我建议稍微清理一下您的代码: - 将对 drawChart 的调用移动到在文档就绪时运行的脚本。 - 从 CSS.

中删除 !important

希望对您有所帮助!