Switch语句不更新Document.getElementById()

时间:2014-07-02 14:52:48

标签: javascript html5 css3

我有一个Switch Statement,看起来像这样。

switch (rsCash.length){
case 1:
    document.getElementById("cash3").innerHTML = rsCash + "Gp";
    break;
case 2:
    document.getElementById("cash3").innerHTML = rsCash + "Gp";
    break;
case 3:
    document.getElementById("cash3").innerHTML = rsCash + "Gp";
    break;
case 4:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut1) + "k";
    break;
case 5:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut2) + "k";
    break;
case 6:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut3) + "k";
    break;
case 7:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut1) + "M";
    break;
case 8:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut2) + "M";
    break;
case 9:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut3) + "M";
    break;
case 10:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut4) + "M";
    break;
case 11:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut5) + "M";
    break;

}

当rsCash变量发生变化时,它不会更新。

HTML是。

<h1 id="RsMoney">RsCash:<b id="cash3">0 Gp</b></h1>

我已经尝试将switch语句放在每秒调用一次的函数中,但它仍然无法正常工作

我已将整个代码复制并粘贴到JavaScript lint中,但未发现任何错误,Google Chrome也未发现任何错误

变量在此处通过此函数更改。

function OpenCrate(){
if(Crates != 0){
var RewardNumber = Math.floor((Math.random() * 15)+ 1);
switch (RewardNumber){
case 1:
    rsCash = rsCash + 200000;
    document.getElementById("Log").innerHTML = "$200k was found!";
    break;
case 2:
    rsCash = rsCash + 25000;
    document.getElementById("Log").innerHTML = "$15 was found!";
    break;
case 3:
    rsCash = rsCash + 5000;
    document.getElementById("Log").innerHTML = "5k was found!";
    break;
case 4:
    rsCash = rsCash + 500000;
    document.getElementById("Log").innerHTML = "500k was found!";
    break;
case 5:
    rsCash = rsCash + 75000;
    document.getElementById("Log").innerHTML = "75k was found!";
    break;
case 6:
    rsCash = rsCash + 5000000;
    document.getElementById("Log").innerHTML = "5m was found!";
    break;
case 7:
    rsCash = rsCash + 275000;
    document.getElementById("Log").innerHTML = "275k was found!";
    break;
case 8:
    rsCash = rsCash + 5000;
    document.getElementById("Log").innerHTML = "5k was found!";
    break;
case 9:
    rsCash = rsCash + 15000;
    document.getElementById("Log").innerHTML = "15k was found!";
    break;
case 10:
    rsCash = rsCash + 8000;
    document.getElementById("Log").innerHTML = "8k was found!";
    break;
case 11:
    rsCash = rsCash + 35000;
    document.getElementById("Log").innerHTML = "35k was found!";
    break;
case 12:
    rsCash = rsCash + 125000;
    document.getElementById("Log").innerHTML = "125k was found!";
    break;
case 13:
    rsCash = rsCash + 80000;
    document.getElementById("Log").innerHTML = "80k was found!";
    break;
case 14:
    rsCash = rsCash + 600000;
    document.getElementById("Log").innerHTML = "600k was found!";
    break;
case 15:
    rsCash = rsCash + 12000;
    document.getElementById("Log").innerHTML = "12k was found!";
    break;
}

1 个答案:

答案 0 :(得分:0)

rsCash是一个数字:

rsCash = rsCash + 200000;

但你正试图占用它的长度:

switch (rsCash.length) ...

你不能这样做 - 你得到undefined,所以你的case都没有。在获取长度之前,您需要将rsCash转换为字符串;像这样的东西:

switch (rsCash.toString().length) ...