简体   繁体   English

jQuery append和Bootstrap轮播

[英]JQuery append and Bootstrap carousel

So I want to add custom amount of carousel items using JQuery. 所以我想使用JQuery添加自定义数量的轮播项目。

Here is the code in script.js (this is also the only thing in the file). 这是script.js中的代码(这也是文件中唯一的代码)。

<script>
$(document).ready(function(){
    for(i = 0; i < 5; i++) {
        $('.carousel-inner').append('<div class="item"> <img src="1.jpg" alt=""> </div>');
    }
});</script>

That is how I call script in 那就是我所谓的脚本

<script type="text/javascript" src="script.js"></script>

Here is the carousel that already contains two items: 这是已经包含两项的轮播:

<div class="carousel-inner" role="listbox">
        <div class="item active">
        <img src="1.jpg" alt="">
        <div class="carousel-caption">
        <h3>Item 1</h3>
        <p>This is the first item</p>
        </div>
        </div>

        <div class="item">
            <img src="5.jpg" alt="">
            <div class="carousel-caption">
                <h3>Item 2</h3>
                <p>This is the second item</p>
            </div>
        </div>
</div>

However, I do not see any other items in the carousel and this does not work. 但是,我在轮播中看不到任何其他项目,因此无法正常工作。

Okay, a silly mistake was made by me, if anyone struggles here's the answer: 好吧,如果有人挣扎,我犯了一个愚蠢的错误,这就是答案:

$('.carousel-indicators').append('<li data-target="#myCarousel" data-slide-to="'+ i + '"></li>');

Just add this in the script.js. 只需将其添加到script.js中即可。 The problem was that I actually created the items, but did not display them, as I need to also add indicators for the items. 问题是我实际上创建了项目,但没有显示它们,因为我还需要为项目添加指示器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM