javascript實現類比時鐘的方法_javascript技巧

來源:互聯網
上載者:User

本文執行個體講述了javascript實現類比時鐘的方法。分享給大家供大家參考。具體實現方法如下:

<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>javascript類比時鐘</title> <!--Javascript範例程式碼解釋: 這個樣本用到了Javascript內建對象Date的getFullYear, getMonth和getDate方法。首先申明一個變數d,var d = new Date(), 表示將當天的日期值賦給變數d。然後使用getFullYear得到年份的值, 用getMonth得到月份值(註:getMonth傳回值範圍為0到11, 所以要得到實際的月份,還要加1), 用getDate得到當天日期所在月份的日期值。 這個樣本還用到了"test?語句1:語句2", 意思是如果符合test條件,那麼執行語句1, 否則使用語句2。計算月和日都用到了這個文法, 如果月和日小於10,在月和日的值之前應該加0。=--> <script type="text/javascript">  function Format2Len(i) {//   if (i < 10) {//    return "0" + i;//   }//   else {//    return i;   //   }   return i < 10 ? "0" + i : i;  }  function RefreshClock() {   var CurrentTime = new Date();   var timeStr = CurrentTime.getFullYear() + "-" +     Format2Len(CurrentTime.getMonth()+1) + "-" +     Format2Len(CurrentTime.getDate()) + " " +     Format2Len(CurrentTime.getHours()) + ":" +     Format2Len(CurrentTime.getMinutes()) + ":" +     Format2Len(CurrentTime.getSeconds());   txtClock.value = timeStr;  }  setInterval("RefreshClock()", 1000); </script></head><body onload="RefreshClock()"><!--頁面載入的時候執行一次--> 目前時間:<input type="text" id="txtClock" /></body></html>

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.