以下是我尝试过的代码
我想要的是 3 秒后文档bgcolor
更改
我用过document.bgcolor
,但它不起作用,因为它总是返回black
var A = new Array("0", "1", "2");
var B = 0;
var C = 3;
function D() {
if (document.images) {
B++;
if (B == C) {
B = 0;
}
document.bgColor = A[B];
setTimeout("D()", 3 * 1000);
}
}
window.onload = function () {
D();
}
您需要以"#"开头的颜色。 例如"#123456"或"#123"。据我所知,您只需将颜色设置为"1"、"2"或"3"。
而不是这些:
var A = new Array("0", "1", "2");
试试这个:
var A = new Array("black", "white", "blue");
Demo:http://jsfiddle.net/gSCEc/
你可以替换
document.bgColor = A[B];
跟
document.body.style.backgroundColor = A[B];
最好使用:
document.body.style.backgroundColor
设置背景颜色的适当方法是:
document.body.style.backgroundColor = somecolour;
自HTML 3.2以来,document.bgColor
就不存在了。
请注意,颜色必须是有效的 CSS 颜色。这意味着它可以是:
- #RRGGBB 形式的 24 位颜色,每个字母代表一个十六进制数字,
- #RGB 形式的 12 位颜色,每个字母再次代表一个十六进制数字,
- 24 位颜色的形式
rgb( R, G, B)
此时每个字母代表 0 到 255 之间的数字, rgba( R, G, B, A)
形式的 32 位颜色,R,G,B 代表 0 到 255 之间的数字,A 是介于 0(透明)和 1(不透明)之间的十进制数。- 颜色名称。有关完整列表,请参阅此页面。
因此,你的字符串"0"
、"1"
、"2"
都行不通。
尝试这样的事情:
(function() {
var colours = ["#ff8080","#80ff80","#8080ff"],
colcount = colours.length, // cache for efficiency reasons
currentcolour = 0; // adjust if you want to start at a different one
document.body.style.backgroundColor = colours[currentcolour];
setInterval(function() {
currentcolour = (currentcolour+1) % colcount;
document.body.style.backgroundColor = colours[currentcolour];
},3000);
})();
你试过使用 javascript 和十六进制颜色代码吗?一个非常好的网站,可以看到锄头十六进制转化为颜色 rapidtables.com
<html>
<head><head/>
<body>
<script language="javascript">
document.bgColor='#2a2a2a';
</script>
</body>
</html>