https://jsbin.com/wonihum/edit?html,js,output

先來說說switch的基本架構

switch( parameter ){

     case 0 :    ;    //  when parameter is 0 , run this case.

          break;

     case 1 :    ;    //  when parameter is 1 , run this case.

          break;

     case 2 :    ;    //  when parameter is 2 , run this case.

          break;

     default;      //If parameter not 0 to 2, run this case.

}

原理:在執行switch時需要先宣告一個變數parameter(d),當電腦在new Date().getDay();

得到值之後對照要執行哪一個case。

例:2019/1/10電腦在new Date().getDay();抓取到4,

就會執行 case 4 區塊, 會執行星期四。

但需要有一個變數來存放"星期四" 這個字串。

所以要在宣告一個變數x來存放這組字串。

當執行完case4 後 break跳離這個switch loop。

利用document.getElementById("demo").innerHTML = x ;

修改id 為 demo的p元素中的HTML內容。

即會顯示  星期四。

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 A&B 的頭像
    A&B

    網頁前端新手上路辛酸史

    A&B 發表在 痞客邦 留言(0) 人氣()