国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

JavaScript switch statement

switch statement is used to perform different actions based on different conditions.


JavaScript switch statement

Use the switch statement to select one of multiple blocks of code to execute.

Syntax

switch(n)
{
case 1:
  執(zhí)行代碼塊 1
break;
case 2:
  執(zhí)行代碼塊 2
break;
default:
 n 與 case 1 和 case 2 不同時執(zhí)行的代碼
}

Working principle: First set the expression n (usually a variable). The value of the expression is then compared to the value of each case in the structure. If there is a match, the code block associated with the case is executed. Please use break to prevent the code from automatically running to the next case.


Example

Displays today’s week name. Please note that Sunday=0, Monday=1, Tuesday=2, etc.:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>php中文網(wǎng)(php.cn)</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="今天是星期日";
                break;
            case 1:x="今天是星期一";
                break;
            case 2:x="今天是星期二";
                break;
            case 3:x="今天是星期三";
                break;
            case 4:x="今天是星期四";
                break;
            case 5:x="今天是星期五";
                break;
            case 6:x="今天是星期六";
                break;
        }
        document.getElementById("demo").innerHTML=x;
    }
</script>
</body>
</html>

Run the program and try it


default Keywords

Please use the default keyword to specify what to do when the match does not exist:

Example

If today If it is not Saturday or Sunday, the default message will be output:

<html>
<head>
    <meta charset="utf-8">
    <title>php中文網(wǎng)(php.cn)</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 6:x="今天是星期六";
                break;
            case 0:x="今天是星期日";
                break;
            default:
                x="期待周末";
        }
        document.getElementById("demo").innerHTML=x;
    }
</script>
</body>
</html>

Run the program and try it



Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</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="今天是星期日"; break; case 1:x="今天是星期一"; break; case 2:x="今天是星期二"; break; case 3:x="今天是星期三"; break; case 4:x="今天是星期四"; break; case 5:x="今天是星期五"; break; case 6:x="今天是星期六"; break; } document.getElementById("demo").innerHTML=x; } </script> </body> </html>
submitReset Code