为什么 Javascript 在 Switch 语句中返回 "undefined"?
Why is Javascript returning "undefined" in Switch statement?
我正在做一个 Javascript 练习来学习 Switch 语句,但我找不到错误在哪里(实际上我不明白为什么它返回“未定义”)。
我试过:
var myNumber = document.querySelector(".myNumber").value;
但没用。
我试过插入一个 case 0,但没用。
var printNumber = document.getElementById("numresult").innerHTML = printNumber;
var myNumber = document.querySelector(".myNumber").textContent;
switch(myNumber) {
case 1:
printNumber = "You got 1";
break;
case 2:
printNumber = "You got 2";
break;
case 3:
printNumber = "You got 3";
break;
case 4:
printNumber = "You got 4";
break;
case 5:
printNumber = "You got 5";
break;
case 6:
printNumber = "You got 6";
break;
case 7:
printNumber = "You got 7";
break;
case 8:
printNumber = "You got 8";
break;
case 9:
printNumber = "You got 9";
break;
default:
printNumber = "Oops";
}
<p class="myNumber">1</p>
<p id="numresult"></p>
您正在比较字符串和数字。
value
returns 一个字符串和 switch
严格比较 ===
.
最后,您需要为元素赋值。
document.getElementById("numresult").innerHTML = printNumber;
var printNumber,
myNumber = +document.querySelector(".myNumber").textContent;
// ^ unary plus to convert string to number
switch (myNumber) {
case 1:
printNumber = "You got 1";
break;
case 2:
printNumber = "You got 2";
break;
case 3:
printNumber = "You got 3";
break;
case 4:
printNumber = "You got 4";
break;
case 5:
printNumber = "You got 5";
break;
case 6:
printNumber = "You got 6";
break;
case 7:
printNumber = "You got 7";
break;
case 8:
printNumber = "You got 8";
break;
case 9:
printNumber = "You got 9";
break;
default:
printNumber = "Oops";
}
document.getElementById("numresult").innerHTML = printNumber;
<p class="myNumber">1</p>
<p id="numresult"></p>
我正在做一个 Javascript 练习来学习 Switch 语句,但我找不到错误在哪里(实际上我不明白为什么它返回“未定义”)。 我试过:
var myNumber = document.querySelector(".myNumber").value;
但没用。 我试过插入一个 case 0,但没用。
var printNumber = document.getElementById("numresult").innerHTML = printNumber;
var myNumber = document.querySelector(".myNumber").textContent;
switch(myNumber) {
case 1:
printNumber = "You got 1";
break;
case 2:
printNumber = "You got 2";
break;
case 3:
printNumber = "You got 3";
break;
case 4:
printNumber = "You got 4";
break;
case 5:
printNumber = "You got 5";
break;
case 6:
printNumber = "You got 6";
break;
case 7:
printNumber = "You got 7";
break;
case 8:
printNumber = "You got 8";
break;
case 9:
printNumber = "You got 9";
break;
default:
printNumber = "Oops";
}
<p class="myNumber">1</p>
<p id="numresult"></p>
您正在比较字符串和数字。
value
returns 一个字符串和 switch
严格比较 ===
.
最后,您需要为元素赋值。
document.getElementById("numresult").innerHTML = printNumber;
var printNumber,
myNumber = +document.querySelector(".myNumber").textContent;
// ^ unary plus to convert string to number
switch (myNumber) {
case 1:
printNumber = "You got 1";
break;
case 2:
printNumber = "You got 2";
break;
case 3:
printNumber = "You got 3";
break;
case 4:
printNumber = "You got 4";
break;
case 5:
printNumber = "You got 5";
break;
case 6:
printNumber = "You got 6";
break;
case 7:
printNumber = "You got 7";
break;
case 8:
printNumber = "You got 8";
break;
case 9:
printNumber = "You got 9";
break;
default:
printNumber = "Oops";
}
document.getElementById("numresult").innerHTML = printNumber;
<p class="myNumber">1</p>
<p id="numresult"></p>