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

轮播图(carousel)无法自动切换求助:首次维护网站遇问题

轮播图无法切换的修复方案

直接给你指出代码里的核心问题,改完就能正常运行:

  1. JS代码未正确包裹:你把$('.carousel').carousel({interval: 5000})直接写在HTML标签里了,这部分JS必须放在<script>标签中,而且要确保在jQuery和Bootstrap JS之后加载。
  2. 重复的active类:
    • 轮播指示器(<ol class="carousel-indicators">下的<li>)只能有一个带active类,你现在两个都加了,得去掉第二个的active。
    • 轮播内容项(<div class="carousel-inner">下的<div class="item">)同理,仅第一个需要加active,第二个要删掉这个类。

修正后的完整代码:

<!-- Header Carousel -->
<header id="myCarousel" class="carousel slide">
    <!-- 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>         
    </ol>

    <!-- Wrapper for slides (v2/legends_treasures.html) -->
    <div class="carousel-inner">
        <div class="item active">
            <a href="">
                <div class="fill" style="background-image:url('https://guernseys.com/Guernseys%20New/images/1900x1080artauction.jpg');"></div> 
            </a>
        </div>      

        <div class="item">
            <a href="">
                <div class="fill" style="background-image:url('https://guernseys.com/Guernseys%20New/images/1900x1080henrymiller.jpg');"></div> 
            </a>
        </div>      
    </div>

    <!-- Controls -->
    <a class="left carousel-control" href="#myCarousel" data-slide="prev">
        <span class="icon-prev"></span>
    </a>
    <a class="right carousel-control" href="#myCarousel" data-slide="next">
        <span class="icon-next"></span>
    </a>
</header> 

<!-- JS代码要放在页面底部,确保先加载jQuery和Bootstrap JS -->
<script>
    $(document).ready(function() {
        $('#myCarousel').carousel({
            interval: 5000
        });
    });
</script>

额外必查项:

确认页面已按顺序引入依赖文件:先jQuery,再Bootstrap的CSS和JS,最后你的自定义JS。示例引入代码:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.4/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js"></script>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 20:55:18