javascript切换使用间隔

Fer*_*SBS 8 javascript intervals switch-statement

我可以在switch语句中使用间隔吗?

喜欢

switch (parseInt(troops[i])) {
                case <10:
                    editbox.style.fontSize = "13px";
                    break;
                case <100:
                    editbox.style.fontSize = "12px";
                    break;
                case <1000:
                    editbox.style.fontSize = "8px";
                    editbox.size = 3;
                    //editbox.style.width = "18px";
                    break;
                default:
                    editbox.style.fontSize = "10px";
            }
Run Code Online (Sandbox Code Playgroud)

???

Cah*_*hit 18

这应该工作:

var j = parseInt(troops[i]);
switch (true) {
            case (j<10):
                editbox.style.fontSize = "13px";
                break;
            case (j<100):
                editbox.style.fontSize = "12px";
                break;
            case (j<1000):
                editbox.style.fontSize = "8px";
                editbox.size = 3;
                //editbox.style.width = "18px";
                break;
            default:
                editbox.style.fontSize = "10px";
        }
Run Code Online (Sandbox Code Playgroud)

  • 为了安全起见,在使用 parseInt() 时应该包含一个基数参数。所以:`parseInt(troops[i],10)`可以避免一些令人抓狂的错误。例如,如果 Forces[i] 的值为“010”,则 js 会猜测基数为 2。可能不是您想要的。 (2认同)