该程序唯一的错误是没有选择颜色,颜色总是黑色,我将单选按钮更改为单个文本输入元素并且它工作,因此错误必须与单选按钮相关。
function hello() {
var name = document.getElementById("name").value;
var para = document.getElementById("para");
var myColor = "";
var color = {
green: document.getElementById("green"),
blue: document.getElementById("blue"),
red: document.getElementById("red")
}
switch (color) {
case color.green.checked:
myColor = "green";
break;
case color.blue.checked:
myColor = "blue";
break;
case color.red.checked:
myColor = "red";
default:
myColor = "black"
}
para.style.color = myColor;
if (name == "") {
para.innerHTML = "Incorrect Input";
} else {
para.innerHTML = "Hello " + name + "!";
}
}
答案 0 :(得分:0)
切换使用您将键值与案例进行比较。在此代码中,它不等于任何情况,因此他选择默认值。
试试这个:
{{1}}