clock.js的簡單介紹

本文目錄一覽:

想在jsp頁面的某個中調用一個獲取系統時間的,div的id設置成了time,寫了一個js函數通過id調用不成功

時間js代碼:Clock.js文件

function Clock() {

var date = new Date();

this.year = date.getFullYear();

this.month = date.getMonth() + 1;

this.date = date.getDate();

this.day = new Array(“星期日”, “星期一”, “星期二”, “星期三”, “星期四”, “星期五”, “星期六”)[date.getDay()];

this.hour = date.getHours() 10 ? “0” + date.getHours() : date.getHours();

this.minute = date.getMinutes() 10 ? “0” + date.getMinutes() : date.getMinutes();

this.second = date.getSeconds() 10 ? “0” + date.getSeconds() : date.getSeconds();

this.toString = function() {

return this.year + “年” + this.month + “月” + this.date + “日 ” + this.hour + “:” + this.minute + “:” + this.second + ” ” + this.day;

};

this.toSimpleDate = function() {

return this.year + “-” + this.month + “-” + this.date;

};

this.toDetailDate = function() {

return this.year + “-” + this.month + “-” + this.date + ” ” + this.hour + “:” + this.minute + “:” + this.second;

};

this.display = function(ele) {

var clock = new Clock();

ele.innerHTML = clock.toString();

window.setTimeout(function() {clock.display(ele);}, 1000);

};

}

需要顯示時間的頁面

SCRIPT src=”js/Clock.js” type=text/javascript/SCRIPT //引入Clock.js文件,注意路徑

label id=clock/label //放入標籤,插入時間

SCRIPT type=text/javascript //實例化clock對象

var clock = new Clock();

clock.display(document.getElementById(“clock”));

/SCRIPT

用JavaScript語言設計:在頁面上顯示當前的時間,格式如圖所示。

script

function tick() {

var hours, minutes, seconds, xfile;

var intHours, intMinutes, intSeconds;

var today;

var Clock = document.getElementById(‘Clock’);

today = new Date();

intHours = today.getHours();

intMinutes = today.getMinutes();

intSeconds = today.getSeconds();

if (intHours == 0) {

hours = “12:”;

xfile = “午夜 “;

} else if (intHours 12) {

hours = intHours+”:”;

xfile = “上午 “;

} else if (intHours == 12) {

hours = “12:”;

xfile = “正午 “;

} else {

intHours = intHours – 12

hours = intHours + “:”;

xfile = “下午 “;

}

if (intMinutes 10) {

minutes = “0”+intMinutes+”:”;

} else {

minutes = intMinutes+”:”;

}

if (intSeconds 10) {

seconds = “0”+intSeconds+” “;

} else {

seconds = intSeconds+” “;

}

timeString = xfile+hours+minutes+seconds;

Clock.innerHTML = timeString;

now = new Date(),hour = now.getHours()

if(hour 6){Clock.innerHTML =timeString+”,明天不用上班了嗎?”}

else if (hour 8){Clock.innerHTML =timeString+”,全新的一天!”}

else if (hour 10){Clock.innerHTML =timeString+”,早安!”}

else if (hour 12){Clock.innerHTML =timeString+”,加油,快午飯了!”}

else if (hour 14){Clock.innerHTML =timeString+”,中午外面的太陽大嗎?”}

else if (hour 16){Clock.innerHTML =timeString+”,喝杯咖啡提提神!”}

else if (hour 18){Clock.innerHTML =timeString+”,加油,快下班了!”}

else if (hour 19){Clock.innerHTML =timeString+”,下班了,回家注意安全!”}

else if (hour 22){Clock.innerHTML =timeString+”,晚上好!”}

else if (hour 24){Clock.innerHTML =timeString+”,夜深了!祝你做個好夢!”}

window.setTimeout(“tick();”, 100);

}

window.onload = tick;

/script

html

body

您好,font color=”#FF0000″現在時間是/fontspan id=”Clock” align=”center” style=”font-size: 12; color:#FF0000″/span

/body

/html

=========================

你會滿意的!!

html顯示時間代碼

時間js代碼:Clock.js文件

function Clock() {

var date = new Date();

this.year = date.getFullYear();

this.month = date.getMonth() + 1;

this.date = date.getDate();

this.day = new Array(“星期日”, “星期一”, “星期二”, “星期三”, “星期四”, “星期五”, “星期六”)[date.getDay()];

this.hour = date.getHours() 10 ? “0” + date.getHours() : date.getHours();

this.minute = date.getMinutes() 10 ? “0” + date.getMinutes() : date.getMinutes();

this.second = date.getSeconds() 10 ? “0” + date.getSeconds() : date.getSeconds();

this.toString = function() {

return this.year + “年” + this.month + “月” + this.date + “日 ” + this.hour + “:” + this.minute + “:” + this.second + ” ” + this.day;

};

this.toSimpleDate = function() {

return this.year + “-” + this.month + “-” + this.date;

};

this.toDetailDate = function() {

return this.year + “-” + this.month + “-” + this.date + ” ” + this.hour + “:” + this.minute + “:” + this.second;

};

this.display = function(ele) {

var clock = new Clock();

ele.innerHTML = clock.toString();

window.setTimeout(function() {clock.display(ele);}, 1000);

};

}

需要顯示時間的頁面

SCRIPT src=”js/Clock.js” type=text/javascript/SCRIPT //引入Clock.js文件,注意路徑

label id=clock/label //放入標籤,插入時間

SCRIPT type=text/javascript //實例化clock對象

var clock = new Clock();

clock.display(document.getElementById(“clock”));

/SCRIPT

html中插入js文件的問題

這個很簡單 你只要把html中script/script裡面的內容剪切出來存在clock.js中

然後在html的head/head這對標籤中加入這句話script src=”clock.js”/script就行了

你本來在script/script 怎麼寫的 在js文件 就怎麼寫 並不是一個文件只能一個函數

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/309474.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2025-01-04 19:31
下一篇 2025-01-04 19:31

相關推薦

  • JS Proxy(array)用法介紹

    JS Proxy(array)可以說是ES6中非常重要的一個特性,它可以代理一個數組,監聽數據變化並進行攔截、處理。在實際開發中,使用Proxy(array)可以方便地實現數據的監…

    編程 2025-04-29
  • Python簡單數學計算

    本文將從多個方面介紹Python的簡單數學計算,包括基礎運算符、函數、庫以及實際應用場景。 一、基礎運算符 Python提供了基礎的算術運算符,包括加(+)、減(-)、乘(*)、除…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • Python海龜代碼簡單畫圖

    本文將介紹如何使用Python的海龜庫進行簡單畫圖,並提供相關示例代碼。 一、基礎用法 使用Python的海龜庫,我們可以控制一個小海龜在窗口中移動,並利用它的「畫筆」在窗口中繪製…

    編程 2025-04-29
  • 解析js base64並轉成unit

    本文將從多個方面詳細介紹js中如何解析base64編碼並轉成unit格式。 一、base64編碼解析 在JavaScript中解析base64編碼可以使用atob()函數,它會將b…

    編程 2025-04-29
  • Node.js使用Body-Parser處理HTTP POST請求時,特殊字元無法返回的解決方法

    本文將解決Node.js使用Body-Parser處理HTTP POST請求時,特殊字元無法返回的問題。同時,給出一些相關示例代碼,以幫助讀者更好的理解並處理這個問題。 一、問題解…

    編程 2025-04-29
  • Python櫻花樹代碼簡單

    本文將對Python櫻花樹代碼進行詳細的闡述和講解,幫助讀者更好地理解該代碼的實現方法。 一、簡介 櫻花樹是一種圖形效果,它的實現方法比較簡單。Python中可以通過turtle這…

    編程 2025-04-28
  • t3.js:一個全能的JavaScript動態文本替換工具

    t3.js是一個非常流行的JavaScript動態文本替換工具,它是一個輕量級庫,能夠很容易地實現文本內容的遞增、遞減、替換、切換以及其他各種操作。在本文中,我們將從多個方面探討t…

    編程 2025-04-28
  • Python大神作品:讓編程變得更加簡單

    Python作為一種高級的解釋性編程語言,一直被廣泛地運用於各個領域,從Web開發、遊戲開發到人工智慧,Python都扮演著重要的角色。Python的代碼簡潔明了,易於閱讀和維護,…

    編程 2025-04-28
  • 用Python實現簡單爬蟲程序

    在當今時代,互聯網上的信息量是爆炸式增長的,其中很多信息可以被利用。對於數據分析、數據挖掘或者其他一些需要大量數據的任務,我們可以使用爬蟲技術從各個網站獲取需要的信息。而Pytho…

    編程 2025-04-28

發表回復

登錄後才能評論