【代码笔记】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>
复制代码

 

 

参考资料:《菜鸟教程》

posted @ 2018-12-19 09:26  花儿迎风笑  阅读(384)  评论(0编辑  收藏  举报