本文目錄一覽:
- 1、想在jsp頁面的某個中調用一個獲取系統時間的,div的id設置成了time,寫了一個js函數通過id調用不成功
- 2、用JavaScript語言設計:在頁面上顯示當前的時間,格式如圖所示。
- 3、html顯示時間代碼
- 4、html中插入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