gpt4 book ai didi

javascript - 使用jquery每3秒显示一次div

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:20 24 4
gpt4 key购买 nike

我有这两个 div,想知道如何每 3 秒显示第二个 (box2) div。

<div id="box1" style="background-color:#0000FF">
<h3>This is a heading in a div element</h3>

<p>This is some text in a div element.</p>
</div>
<div id="box2" style="background-color:red">
<h3>This is a heading in a div element</h3>

我怎样才能用 jquery 做到这一点?

我在这里创建了一个 fiddle 。 http://jsfiddle.net/jRmrp/5/

更新 1

Khanh TO 给出的答案有效,但我想知道当 div 计数超过 2 时该怎么办。它只允许两个。

最佳答案

你需要这个吗?

setInterval(function(){
$("#box2").toggle();
$("#box1").toggle();
},3000);

DEMO

根据新要求更新:

var currentIndex = 0;
$(".box:not(:eq("+ currentIndex +"))").hide();

var totalDiv = $(".box").length;

setInterval(function(){
currentIndex = (currentIndex + 1) % totalDiv;

$(".box").hide();
$(".box").eq(currentIndex).show();

},3000);

DEMO

关于javascript - 使用jquery每3秒显示一次div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20157790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com