我是一名初学程序员,我确定答案正在盯着我,但我无法弄清楚我做错了什么。它只是一个简单的程序,让用户输入三种类型的天气之一,并根据输入文本字段的内容,程序将回答适当类型的鞋类。我一直在研究它几个小时,在这里和那里调整,但我不能让它工作。有任何想法吗?这是代码:
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>What Shoes Should I Wear?</title>
<script type="text/javascript">
function shoeWeather() {
var shoe;
switch (weather) {
case 'hot':
shoe = "sandals";
break;
case 'rain':
shoe = "galoshes";
break;
case 'snow':
shoe = "boots";
break;
default:
shoe = "shoes";
break;
return;
document.getElementById('shoe').innerHTML = shoes;
}
}
</script>
</head>
<body>
Weather:<input type="text" id="weather">
<button type="button" onclick="shoeWeather()">What's the Right Shoe for the Job?</button>
<div id="shoe"></div>
</body>
</html>
答案 0 :(得分:1)
weather
未定义。您必须读取输入值,然后操作开关。document.getElementById('shoe').innerHTML = shoes;
放在开关内,在返回语句之前并使用错误的变量(鞋子应该是鞋子)。尝试这样的事情:
尝试:
var shoe='';
switch (document.getElementById('weather').value){
//set shoe based on switch cases
}
document.getElementById('shoe').innerHTML = shoe;