JS if语句 - innerHTML [英] JS if statement - innerHTML

查看:168
本文介绍了JS if语句 - innerHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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>";
        }
    }
}

它应检查是否有a 1或0并用绿色或红色圆圈替换它,但我只得到红色圆圈。

It should check if there is a 1 or 0 and replace it with a green or red circle, but I only get red circles.

stat打印给了我一个0 0 0 1 1 1 1 1,但似乎没有使用if语句。有人知道为什么吗?

"stat" printing gave me an " 0 0 0 1 1 1 1 1" but it seem to be not working with the if statement. Does anybody know why?

推荐答案

如果 stat.length 总是等于1 :

if(stat[0]=='1'){

如果你要找的只是一个 1 in stat

If all you are looking for is a 1 in stat:

if(stat.match(/1/)){//contains a 1, may contain more

这篇关于JS if语句 - innerHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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