【代码笔记】Web-JavaScript-JavaScript switch语句
一,效果图。
二,代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>javascript switch语句</title>
</head>
<body>
<p>点击下面的按钮来显示今天是周几:</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x;
var d = new Date().getDay();
switch (d) {
case 0:
x = "Today it's Sunday";
break;
case 1:
x = "Today it's Monday";
break;
case 2:
x = "Today it's Tuesday";
break;
case 3:
x = "Today it's Wednesday";
break;
case 4:
x = "Today it's Thursday";
break;
case 5:
x = "Today it's Friday";
break;
case 6:
x = "Today it's Saturday";
break;
}
document.getElementById("demo").innerHTML = x;
}
</script>
<p>点击下面的按钮,会显示出基于今日日期的消息</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x;
var d = new Date().getDay();
switch (d) {
case 6:
x = "today is saturday";
break;
case 0:
x = "today is sunday";
break;
default:
x = "looking forward to the weekend";
}
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
参考资料:《菜鸟教程》
本文来自博客园,作者:花儿迎风笑,转载请注明原文链接:https://www.cnblogs.com/yang-guang-girl/p/10141288.html