javascript萬年曆

<html lang="zh-cn">

 <head>

   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">  

   <meta http-equiv="Cache-Control" content="no-siteapp" /> 

   <style type="text/css">

     div{

        position: absolute;

top: 0;

left: 600px;

     }

     h5{

margin-left: 88px;

     }

     table{

text-align: center;

border: 1px solid #aaa; 

width: 273px; 

height: 158px;

     }

     table td{

width: 39px;

height: 23px;

border: 1px solid #aaa;

    }

   </style>

 </head>

 <body>

  <div>

  <script type="text/javascript">

     var today = new Date();

     var date = today.getDate();

     var theday = 1;

     document.write("<h5>"+today.getFullYear()+"年"+(today.getMonth()+1)+"月"+"</h5>");

     document.write("<table><tr>");

     document.write("<td><font color='red'>Sun</font></td>");

     document.write("<td>Mon</td>");

     document.write("<td>Tue</td>");

     document.write("<td>Wed</td>");

     document.write("<td>Thu</td>");

     document.write("<td>Fir</td>");

     document.write("<td><font color='red'>Sat</font></td></tr>");

     var month = today.getMonth()+1;

     var max;

     switch(month){

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

 max = 31;

break;

case 4:

case 6:

case 9:

case 11:

 max = 30;

break;

default:

 max = 28;

     }

     today.setDate(1);

     var temp = today.getDay();

     for ( var i = 0; i < 6;i++) {

        document.write("<tr>");

for ( var j = 0; j < 7; j++) {

  document.write("<td>");

  if( (j+1) > temp ) {  //當天j+1小於temp時不執行代碼

     temp = 0;         //temp賦值爲0後下一個j可以從0開始執行代碼

     if( theday <= max){

if(theday == date) {

   document.write("<font color='green'>["+theday+"]</font>");

        }

else if( j== 0 || j==6 ) {

   document.write("<font color='red'>"+theday+"</font>");

}

else{

   document.write("<font color='blue'>"+theday+"</font>")

}

     }

     theday++;

           }

         document.write("</td>");

        }

      document.write("</tr>");

     }

     document.write("</table>"); 

 </script>

 </body>

</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章