自动更改div或div的内容javascript

时间:2015-05-10 21:09:26

标签: javascript

我想要一个自动更改整个div或div内容的脚本。请,只需javascript,注意jQuery。

我试过了:

<script type="text/javascript">
var kickoff = new Date("May 10, 2015 23:18:00"),
timerContainer = document.getElementById('timerContent'),
timer; // Added for visibility in the global scope.

function cdtd() {

 var now = new Date(),
  timeDiff = kickoff.getTime() - now.getTime();

if (timeDiff <= 0) {

 clearTimeout(timer);
 timerContainer.innerHTML = '<div>KICK OFF!!!!</div> <img src="img1.gif" />';

} else {

// This style of declaration is just preference.
var seconds = Math.floor(timeDiff / 1000),
    minutes = Math.floor(seconds / 60),
    hours = Math.floor(minutes / 60),
    days = Math.floor(hours / 24);

if (seconds < 10) seconds = "0" + seconds;
if (minutes < 10) minutes = "0" + minutes;
if (hours < 10) hours = "0" + hours;
if (days < 10) days = "0" + days;

// Using an array simplifies the process of creating the text
// In some browsers this is more performant than using '' + ''
// In other browsers it's not, in reality the difference marginal in
// small iterations like this one.
var textTemplateArray = [
      days, 'Days',
      minutes, 'Minutes',
      hours, 'Hours',
      seconds, 'Seconds'
    ];

timerContainer.innerHTML = textTemplateArray.join(' ');
     }
}

timer = setInterval(cdtd, 1000);

我不知道如何用<body>元素编写代码工作。我想把一个div用几秒钟改成另一个div,等等......用3-4个div。

1 个答案:

答案 0 :(得分:0)

我认为我正确地假设您希望javascript将<div>元素中的文本更改为某个变量的值,并在每秒/任何时间帧重复此操作以更新显示的值。< / p>

可以这样做:

setInterval(function(){document.getElementById('id').innerHTML = 'New text'},1000);

将每秒重复以下(1000毫秒):

document.getElementById('id').innerHTML = 'New text'

其中'newtext'应替换为变量名称