如何使下一个和上一个按钮起作用? (轮播 Bootstrap)

How can I made the next and prev buttons work? (Carousel Bootstrap)

我正在制作一个没有图像的旋转木马,只有 bootstrap 中的文本,但是当我单击“下一个”和“上一个”按钮时,滑块不起作用。

有人能帮忙吗?

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div id="slider-2 carousel-1" class="carousel slide" data-bs-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <div class="caption-2">
        <h5>Lorem ipsum, dolor sit amet</h5>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore suscipit odit reprehenderit dolores, est minus. Iusto omnis corporis, optio, facere cupiditate illum earum molestiae vitae facilis repudiandae possimus sit impedit?</p>
      </div>
    </div>
    <div class="carousel-item">
      <div class="caption-2">
        <h5>Lorem ipsum, dolor sit amet</h5>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore suscipit odit reprehenderit dolores, est minus. Iusto omnis corporis, optio, facere cupiditate illum earum molestiae vitae facilis repudiandae possimus sit impedit?</p>
      </div>
    </div>
    <div class="carousel-item">
      <div class="caption-2">
        <h5>Lorem ipsum, dolor sit amet</h5>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore suscipit odit reprehenderit dolores, est minus. Iusto omnis corporis, optio, facere cupiditate illum earum molestiae vitae facilis repudiandae possimus sit impedit?</p>
      </div>
    </div>
  </div>
  
  <button class="carousel-control-prev btn-carousel" type="button" data-bs-target="#slider-2" data-bs-slide="prev">
      <span class="carousel-control-prev-icon seta-3" aria-hidden="true"></span>
      <span class="visually-hidden">Previous</span>
  </button>
<button class="carousel-control-next btn-carousel" type="button" data-bs-target="#slider-2" data-bs-slide="next">
      <span class="carousel-control-next-icon seta-4" aria-hidden="true"></span>
      <span class="visually-hidden">Next</span>
  </button>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

这很可能导致您的问题:

<div id="slider-2 carousel-1"

不要在单个元素中使用多个 ID。有些浏览器可能工作正常,但有些可能不工作。

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div id="slider-2" class="carousel slide" data-bs-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <div class="caption-2">
        <h5>Lorem ipsum, dolor sit amet</h5>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore suscipit odit reprehenderit dolores, est minus. Iusto omnis corporis, optio, facere cupiditate illum earum molestiae vitae facilis repudiandae possimus sit impedit?</p>
      </div>
    </div>
    <div class="carousel-item">
      <div class="caption-2">
        <h5>Lorem ipsum, dolor sit amet</h5>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore suscipit odit reprehenderit dolores, est minus. Iusto omnis corporis, optio, facere cupiditate illum earum molestiae vitae facilis repudiandae possimus sit impedit?</p>
      </div>
    </div>
    <div class="carousel-item">
      <div class="caption-2">
        <h5>Lorem ipsum, dolor sit amet</h5>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore suscipit odit reprehenderit dolores, est minus. Iusto omnis corporis, optio, facere cupiditate illum earum molestiae vitae facilis repudiandae possimus sit impedit?</p>
      </div>
    </div>
  </div>
  
  <button class="carousel-control-prev btn-carousel" type="button" data-bs-target="#slider-2" data-bs-slide="prev">
      <span class="carousel-control-prev-icon seta-3" aria-hidden="true"></span>
      <span class="visually-hidden">Previous</span>
  </button>
<button class="carousel-control-next btn-carousel" type="button" data-bs-target="#slider-2" data-bs-slide="next">
      <span class="carousel-control-next-icon seta-4" aria-hidden="true"></span>
      <span class="visually-hidden">Next</span>
  </button>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>