在一段时间后更改div的背景颜色 [英] Change the background color of a div after some interval
本文介绍了在一段时间后更改div的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿,我想改变一个div的背景颜色。我写了一个脚本来改变身体的背景颜色。代码如下
< script>
colors = new Array('black','red','green');
function annoyingEffect(tic){
tic%= colors.length;
document.bgColor = colors [tic];
setTimeout(annoyingEffect(+(tic + 1)+),2000);}
< / script>
< body onload =annoyingEffect(0)>
< / body>
但是如果我尝试添加一个div并插入onload =annoyingEffect(0)它,它不工作。
rel =nofollow>这是一个例子(http://jsfiddle.net/NSJDR/5/)如何改变正文和< div>
: < html>
< head>
< script type =text / javascript>
colors = new Array('blue','red','green','yellow','brown','orange');
function annoyingEffect(tic){
tic%= colors.length;
document.bgColor = colors [tic];
setTimeout(annoyingEffect(+(tic + 1)+),1000);
}
function annoyingEffectOnDiv(tic,divId){
tic%= colors.length;
divVar = document.getElementById(divId);
divVar.style.background = colors [tic];
setTimeout(annoyingEffectOnDiv(+(tic + 1)+,'+ divId +'),1000);
}
< / script>
< / head>
< body onload =annoyingEffect(0); annoyingEffectOnDiv(2,'divtest');>
< div id =divtest>
< label> HELLOOOOOA:D< / label>
< / div>
< / body>
< / html>
Hey i am trying to change the background color of a div. I have written a script to change the background color of the body. The code is below
<script>
colors = new Array('black', 'red', 'green');
function annoyingEffect(tic){
tic %= colors.length;
document.bgColor = colors[tic];
setTimeout("annoyingEffect("+(tic+1)+")", 2000);}
</script>
<body onload="annoyingEffect(0)">
</body>
But if i try to add a div and insert the onload="annoyingEffect(0)" function inside it, it doesn't work. What I'm i missing?
解决方案
Here's an example (http://jsfiddle.net/NSJDR/5/) how you change the body and a <div>
:
<html>
<head>
<script type="text/javascript">
colors = new Array('blue', 'red', 'green', 'yellow', 'brown', 'orange');
function annoyingEffect(tic){
tic %= colors.length;
document.bgColor = colors[tic];
setTimeout("annoyingEffect("+(tic+1)+")", 1000);
}
function annoyingEffectOnDiv(tic, divId){
tic %= colors.length;
divVar = document.getElementById(divId);
divVar.style.background = colors[tic];
setTimeout("annoyingEffectOnDiv("+(tic+1)+", '"+divId+"')", 1000);
}
</script>
</head>
<body onload="annoyingEffect(0); annoyingEffectOnDiv(2, 'divtest');">
<div id="divtest">
<label>HELLOOOOOA :D</label>
</div>
</body>
</html>
这篇关于在一段时间后更改div的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文