未定义不是document.getElementById(“Id”)的函数

时间:2014-07-03 03:34:00

标签: javascript jquery html

我不断获得相同的错误消息"未捕获的TypeError:undefined不是函数"有这个吗? 变量为rsCash = 5;

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

并拥有此javascript

switch (rsCash){
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;

}

1 个答案:

答案 0 :(得分:0)

这意味着rsCash不具备substring类型string的方法。

您可以尝试输入rsCash

`function test(a){
  switch(a){
  case 1: console.log(typeof a);break;
 }
}`

您可以获得number,而不是string,因此您应该将rsCash更改为string类型