You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bootstrap轮播组件prev按钮失效,点击跳转新页面问题求助

Bootstrap轮播prev按钮跳转页面而非切换的解决方法

问题出在prev按钮的href属性上——你漏写了前缀#,导致浏览器将其识别为普通页面链接而非锚点,所以点击时会跳转页面,而不是触发轮播切换。

对比你的代码:

  • next按钮的href="#testimonial-carousel"是正确的,带了#指向轮播容器的ID
  • prev按钮的href="testimonial-carousel"缺少#,这就是错误根源

修正后的代码片段:

<div id="testimonial-carousel" class="carousel slide" data-ride="false">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <h2>I no longer have to sniff other dogs for love. I've found the hottest Corgi on TinDog. Woof.</h2>
      <img class="testimonial-image" src="images/dog-img.jpg" alt="dog-profile">
      <em>Pebbles, New York</em>
    </div>
    <div class="carousel-item">
      <h2 class="testimonial-text">My dog used to be so lonely, but with TinDog's help, they've found the love of their life. I think.</h2>
      <img class="testimonial-image" src="images/lady-img.jpg" alt="lady-profile">
      <em>Beverly, Illinois</em>
    </div>
  </div>
  <!-- 修正prev按钮的href,添加#前缀 -->
  <a class="carousel-control-prev" href="#testimonial-carousel" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </a>
  <a class="carousel-control-next" href="#testimonial-carousel" role="button" data-slide="next">
    <span class="carousel-control-next-icon"></span>
  </a>
</div>

Bootstrap轮播的控制按钮需要通过带#的锚点href关联到轮播容器的ID,这样组件才能捕获点击事件并执行切换逻辑,否则会触发浏览器默认的链接跳转行为。

内容的提问来源于stack exchange,提问作者Anoushka Panda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 13:05:27