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
相关产品推荐
相关产品推荐

