每隔30秒用另一个div替换一个div

时间:2015-05-07 14:35:11

标签: javascript jquery html css

我的代码:

<div class="employee">
<div class="text">Number of Employees</div>
<div class="number">50</div>
</div>

我想要下面的div,每隔30秒替换上面的div。即上面的div显示30秒,然后下面的div显示30秒,然后返回上面的div 30秒,依此类推....

<div class="recruit">
<div class="text">Recruiting</div>
<div class="number">1</div>
</div>

我知道如何执行此操作 -

$( ".employee" ).click(function() {
$( this ).replaceWith( "<div class="recruit"><div class="text">Recruiting</div><div class="number">1</div></div>" );
});

但我不知道如何自动更改每30秒更换一次?

1 个答案:

答案 0 :(得分:1)

您可以使用:

setInterval(function(){
   if($('.employee').length)
      $('.employee').replaceWith( '<div class="recruit"><div class="text">Recruiting</div><div class="number">1</div></div>' );
    else
      $('.recruit').replaceWith( '<div class="employee"><div class="text">Number of Employees</div><div class="number">50</div></div>' );
},30000);

<强> Working Demo with 3 seconds toggling