使用Javascript定时器更改背景颜色和文本颜色 [英] Change the background color and text color with a timer with Javascript

查看:1131
本文介绍了使用Javascript定时器更改背景颜色和文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个计时器来改变表格及其所有单元格的背景和文字颜色。我有下面的脚本在结束标记之前。背景是唯一的变化。表的id是'titleTable'。感谢

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

<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>


推荐答案

(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);
})();

在线演示 http://jsfiddle.net/Dzk2h/2/

只要将上述程式码您网页底部的< script> 元素。

Just put the above code inside a <script> element at the bottom of your page.

这篇关于使用Javascript定时器更改背景颜色和文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆