基於javascript實現動態顯示當前系統時間,javascript當前

來源:互聯網
上載者:User

基於javascript實現動態顯示當前系統時間,javascript當前

本文執行個體講解了javascript實現動態顯示當前系統時間的詳細代碼,具體內容如下

  • (1)時間日期資訊,應該在一個<div>中來顯示
  • (2)定時器:每隔一秒再次訪問系統時間,window對象的setTimeout()
  • (3)時鐘顯示的時機(事件):當網頁載入完成後才顯示,事件onload
  • (4)如何將 時間日期資訊 寫入到指定的<div>中,DOM對象中的innerHTML 屬性

具體代碼:

<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>無標題文檔</title><script type="text/javascript">//定義函數:構建要顯示的時間日期文字function showTime(){ //建立Date對象 var today = new Date(); //分別取出年、月、日、時、分、秒 var year = today.getFullYear(); var month = today.getMonth()+1; var day = today.getDate(); var hours = today.getHours(); var minutes = today.getMinutes(); var seconds = today.getSeconds(); //如果是單個數,則前面補0 month  = month<10  ? "0"+month : month; day  = day <10  ? "0"+day : day; hours  = hours<10  ? "0"+hours : hours; minutes = minutes<10 ? "0"+minutes : minutes; seconds = seconds<10 ? "0"+seconds : seconds;  //構建要輸出的字串 var str = year+"年"+month+"月"+day+"日 "+hours+":"+minutes+":"+seconds;  //擷取id=result的對象 var obj = document.getElementById("result"); //將str的內容寫入到id=result的<div>中去 obj.innerHTML = str; //延時器 window.setTimeout("showTime()",1000);}</script><style type="text/css">#result{ width:500px; border:1px solid #CCCCCC; background:#FFFFCC; margin:50px auto; font-size:24px; color:#FF0000; padding:20px;}</style></head><body onload="showTime()"><div id="result"></div></body></html>

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

您可能感興趣的文章:
  • javascript 顯示當前系統時間代碼
  • js顯示當前系統時間的代碼
  • js即時擷取系統目前時間執行個體代碼
  • jsp實現頁面即時顯示當前系統時間的方法
  • JS之Date對象和擷取系統目前時間詳解
  • 使用iojs的jsdom庫實現同步系統時間
  • js獲得當前系統日期時間的方法
  • javascript擷取系統目前時間的方法
  • 6種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.