我有这两个 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);
根据新要求更新:
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);
关于javascript - 使用jquery每3秒显示一次div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20157790/