【发布时间】:2013-05-28 16:37:28
【问题描述】:
function colorize(){
for(i = 1; i<=8; i++)
{
var id = document.getElementById('op' + i);
var stat = id.innerHTML;
document.write(stat + " ");
if(stat == 1)
{
id.innerHTML = "<div style='background:#00FF00; width: 20px; height: 20px; border-radius: 15px;'></div>";
}
else{
id.innerHTML = "<div style='background:#FF0000; width: 20px; height: 20px; border-radius: 15px;'></div>";
}
}
}
它应该检查是否有 1 或 0 并将其替换为绿色或红色圆圈,但我只得到红色圆圈。
“stat”打印给了我一个“0 0 0 1 1 1 1 1”,但它似乎不适用于 if 语句。有人知道为什么吗?
【问题讨论】:
-
stat 是一个字符串,1 是一个数字。试试 stat == "1"
-
试过了,结果还是一样
-
@Thilo,是的,你是对的,它可能是 innerHtml 中的一个空格......
-
虽然也不是这样......
-
你能在 if 和 else 分支中添加一个 document.write 并发布结果吗? innerHtml 中是否有单个 1 或 0?
标签: javascript if-statement innerhtml