Bootstrap 基于幻灯片索引的轮播事件

Bootstrap Carousel event based on slide index

我有这个简单的 html 页面。

<html>
<head>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
    <body>
        <div id="MyCarousel" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img height="500px" class="d-block w-100" src="1.jpg" alt="First">
    </div>
    <div class="carousel-item">
      <img height="500px" class="d-block w-100" src="2.jpg" alt="Second">
    </div>
    <div class="carousel-item">
      <img height="500px" class="d-block w-100" src="3.jpg" alt="Third">
    </div>
  </div>
  <a class="carousel-control-prev" href="#MyCarousel" 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="#MyCarousel" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
  <img id="test" height="100px" width="100px" src="4.png" alt="Fourth">
  <p id="test2" >This is a paragraph.</p>
</div>
<script>
$('#MyCarousel').on('slide.bs.carousel', function onSlide (ev) {
  var id = ev.relatedTarget.id;
  switch (id) {
    case "1":
      window.alert(1);
      break;
    case "2":
      window.alert(2);
      break;
    case "3":
      window.alert(3);
      break;
    default:
      window.alert(4);
  }
});
</script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    </body>
</html>

轮播 ID 是#MyCarousel
我正在使用 Bootstrap 4.0.0
我想要的是当我改变旋转木马幻灯片时显示一个警报。
但是它不起作用,我该如何解决?
可能是onSlide函数出错了

onSlide 事件没有工作,因为应用程序无法识别 jQuery CDN 参考 。当我进行以下编辑时显示警告。但是,您不能直接从变量 ev.relatedTarget.

中读取 id

$('#MyCarousel').on('slide.bs.carousel', function onSlide (ev) {
  var id = ev.relatedTarget.id;
  console.log(ev);
  switch (id) {
    case "1":
      window.alert(1);
      break;
    case "2":
      window.alert(2);
      break;
    case "3":
      window.alert(3);
      break;
    default:
      window.alert(4);
  }
});
<html>
<head>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
    <body>
        <div id="MyCarousel" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img height="500px" class="d-block w-100" src="https://picsum.photos/200/300" alt="First">
    </div>
    <div class="carousel-item">
      <img height="500px" class="d-block w-100" src="https://picsum.photos/200/300" alt="Second">
    </div>
    <div class="carousel-item">
      <img height="500px" class="d-block w-100" src="https://picsum.photos/200/300" alt="Third">
    </div>
  </div>
  <a class="carousel-control-prev" href="#MyCarousel" 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="#MyCarousel" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
  <img id="test" height="100px" width="100px" src="https://picsum.photos/200/300" alt="Fourth">
  <p id="test2" >This is a paragraph.</p>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

您的主要问题是您在导入之前尝试使用 jQuery。如果您在 JS 之前导入它,您的代码将 运行 并启动警告框。所以你的解决方案是在任何调用 $

之前导入 <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

但是,您仍然需要处理识别 id 的代码。如果您仍然遇到困难,很乐意为您提供进一步的帮助,请告诉我。