使用带有 Javascript 的计时器更改背景颜色和文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4925099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the background color and text color with a timer with Javascript
提问by Chewie The Chorkie
I'm trying to change both the background and text color of a table and all its cells with a timer. I have the script below just before the end tag. The background is the only thing that changes. The id of the table is 'titleTable'. Thanks
我正在尝试使用计时器更改表格及其所有单元格的背景和文本颜色。我在结束标记之前有下面的脚本。背景是唯一改变的东西。表的 id 是“titleTable”。谢谢
<script language="Javascript">
<!-- Begin
titleTable.bgColor='#FFFFFF';
setInterval("Timer()", 500);
x=1;
function Timer() {
set=1;
if(x==0 && set==1) {
titleTable.bgColor='#000000';
titleTable.style.color='#FFFFFF';
x=1;
set=0;
}
if(x==1 && set==1) {
titleTable.bgColor='#FFFFFF';
titleTable.style.color='#000000';
x=0;
set=0;
}
}
// End -->
</script>
回答by ?ime Vidas
(function() {
var s = document.getElementById('titleTable').style,
f = false,
c1 = '#000000',
c2 = '#ffffff';
setInterval(function() {
s.backgroundColor = f ? c1 : c2;
s.color = f ? c2 : c1;
f = !f;
}, 500);
})();
Live demo:http://jsfiddle.net/Dzk2h/2/
现场演示:http : //jsfiddle.net/Dzk2h/2/
Just put the above code inside a <script>element at the bottom of your page.
只需将上面的代码放在<script>页面底部的元素中。
回答by kirilloid
var titleTable = document.getElementById('titleTable');
var titleTable = document.getElementById('titleTable');
if(x==0 && set==1)
->
if((x==0) && (set==1))
if(x==0 && set==1)
->
if((x==0) && (set==1))
Just use "blink" tag =)
只需使用“闪烁”标签 =)
Ah, "The background is the only thing that changes". Check styles. If you have CSS rule like
啊,“背景是唯一改变的东西”。检查样式。如果你有类似的 CSS 规则
#titleTable td { color: black; }
It will not be overriden by setting inline style to the table.
它不会通过为表格设置内联样式而被覆盖。

