开发者

javascript switch using intervals

开发者 https://www.devze.com 2023-01-02 10:37 出处:网络
Can I use intervals in a switch statement? Like switch (parseInt(troops[i])) { case <10: editbox.style.fontSize = \"13px\";

Can I use intervals in a switch statement?

Like

switch (parseInt(troops[i])) {
                case <10:
                    editbox.style.fontSize = "13px";
                    break;
                case <100:
                    editbox.style.fontSize = "12px";
                    break;
                case <1000:
    开发者_C百科                editbox.style.fontSize = "8px";
                    editbox.size = 3;
                    //editbox.style.width = "18px";
                    break;
                default:
                    editbox.style.fontSize = "10px";
            }

???


This should work though:

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


No. switch can be used only with discrete values. For ranges you'll have to use an if statement.

var val = parseInt(troops[i]);
if (val < 10) {
    editbox.style.fontSize = "13px";
} else if (val < 100) { 
    // ...
} else {
}


Sometimes a switch is too much like work

var j= parseInt(troops[i]),
val= j<10? 13: j<100? 12: j<1000? 8: 10;

editbox.style.fontSize= val+'px';
if(val== 8) editbox.size= 3;
0

精彩评论

暂无评论...
验证码 换一张
取 消