用Switch写一个输入数字显示星期的html
时间: 2023-05-23 09:03:00 浏览: 108
代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<title>输入数字显示星期</title>
</head>
<body>
<label for="num">请输入数字:</label>
<input type="number" id="num">
<button onclick="displayWeekday()">确定</button>
<p id="weekday"></p>
<script>
function displayWeekday() {
var num = document.getElementById("num").value;
var weekday;
switch(num) {
case "1":
weekday = "星期一";
break;
case "2":
weekday = "星期二";
break;
case "3":
weekday = "星期三";
break;
case "4":
weekday = "星期四";
break;
case "5":
weekday = "星期五";
break;
case "6":
weekday = "星期六";
break;
case "7":
weekday = "星期日";
break;
default:
weekday = "请输入1-7的数字";
break;
}
document.getElementById("weekday").innerHTML = weekday;
}
</script>
</body>
</html>
```
当用户在输入框中输入数字并点击“确定”按钮后,会根据输入的数字使用 Switch 语句来判断对应的星期几,然后在 HTML 页面上显示出来。
希望可以解决您的问题。
阅读全文