我想在页面上创建2个链接:
以下是我认为应该有效的javascript代码:
var nIntervId;
function changeColor() {
nIntervId = setInterval(flashColor, 500);
}
function flashColor() {
var oElem = document.getElementById("hidden");
var index = Math.round(Math.random() * 9);
var ColorValue;
if(index == 1)
ColorValue = "FFCCCC";
if(index == 2)
ColorValue = "CCAFFF";
if(index == 3)
ColorValue = "A6BEFF";
if(index == 4)
ColorValue = "99FFFF";
if(index == 5)
ColorValue = "D5CCBB";
if(index == 6)
ColorValue = "99FF99";
if(index == 7)
ColorValue = "FFFF99";
if(index == 8)
ColorValue = "FFCC99";
if(index == 9)
ColorValue = "CCCCCC";
oElem.style.backgroundColor=ColorValue;
}
function stopColor() {
clearInterval(nIntervId);
}
以下是调用它的html代码:
<a href="javascript:void(0)" onclick="changeColor();" style="color:#FFF;">Change</a>
<a href="javascript:void(0)" onclick="stopColor();" style="color:#FFF;">STOP</a>
但它根本不起作用。我忘了什么吗?
感谢您的帮助
安迪
答案 0 :(得分:2)
您需要在颜色值
之前添加“#”喜欢“#CCCCCC”