将变量设置为返回为Null的字符串 [英] Setting a Variable to a string returning as Null

查看:128
本文介绍了将变量设置为返回为Null的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有这些变量

var rsCash = 0;
var rsCashString = rsCash.toString();
var rsCashLength = rsCashString.length;

我在switch语句中使用rsCash变量,我需要的数字的长度,获取错误消息未捕获TypeError:无法将属性'innerHTML'设置为null

I am using the rsCash Variable in a Switch Statement and i need the length of the number but i am getting the Error Message "Uncaught TypeError: Cannot set property 'innerHTML' of null

这是Switch语句

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

}

p>

I have Tried

Switch (rsCash.toString().length){

我也有一个switch语句,它像这样改变rsCash变量

I also have a Switch Statement which changes the rsCash Variable like this

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;

}

我不明白为什么变量返回null?

I Don't understand why the variable is returning null?

推荐答案

错误指向此代码(重复多次):

The error is pointing you to this code (repeated many times):

document.getElementById("cash3").innerHTML = rsCash + "Gp";

document.getElementById(cash3) call在这里返回null。您需要正确找到您的元素。

The document.getElementById("cash3") call is returning null here. You need to properly find your element.

您可能只需要这样做:

var cashElement = document.getElementById("correctID");

然后重复使用该变量。

像注释者说的,这很可能是因为你在加载DOM之前执行代码。 jQuery有简单的方法来处理这个,通过在 $(document).ready 中放置代码,或者可以在vanilla javascript:

Like commenters have said, it's likely that this is because you are executing the code before the DOM is loaded. jQuery has simple ways to handle this by placing code in $(document).ready, or it can be done in vanilla javascript:

document.addEventListener("DOMContentLoaded", function() {
  // code…
});

这将允许您的代码在DOM加载后运行,使您的代码能够看到HTML并访问它。

This will allow your code to run after the DOM is loading, making your code able to see the HTML and access it.

这篇关于将变量设置为返回为Null的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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