gpt4 book ai didi

javascript - 将变量设置为以 Null 返回的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 13:13:26 25 4
gpt4 key购买 nike

我现在有这些变量

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

我在 Switch 语句中使用 rsCash 变量,我需要数字的长度,但我收到错误消息“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;

我试过了

Switch (rsCash.toString().length){

我还有一个 Switch 语句,它像这样更改 rsCash 变量

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?

最佳答案

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

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

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

你可能只需要这样做:

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

然后在整个过程中重用该变量。

正如评论者所说,这很可能是因为您在加载 DOM 之前执行代码。 jQuery 有简单的方法来处理这个问题,方法是将代码放在 $(document).ready 中,或者可以用 vanilla javascript 完成:

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

这将允许您的代码在加载 DOM 后运行,从而使您的代码能够查看和访问 HTML。

关于javascript - 将变量设置为以 Null 返回的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24539435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com