(1):首先用JS編寫實時顯示系統時間的函數 clockon() 只有一個參數bgclock,用於指定顯示用於轉化後的<div>標記的名稱,無返回值,大家進行網站開發時可以將改函數保存在JS文件中,以便重用 代碼如下:
復制代碼 代碼如下:
<script>
function clockon(bgclock)
{
var now = new Date();
var year = now.getYear();
var month = now.getMonth();
var date = now.getDate();
var day = now.getDay();
var hour = now.getHours();
var minu = now.getMinutes();
var sec = now.getSeconds();
var week;
month = month+1;
if(month<10)month="0"+month;
if(date<10)date="0"+date;
if(hour<10)hour="0"+hour;
if(minu<10)minu="0"+minu;
if(sec<10)sec="0"+sec;
var arr_week = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
week = arr_week[day];
var time = "";
time = year+"年"+month+"月"+date+"日"+week+""+hour+";"+minu+";"+sec;
if(document.all)
{
bgclock.innerHTML="["+time+"]"
}
var timer = setTimeout("clockon(bgclock)",200);
}
</script>
(2):在頁面的<body> 的onload事件中 調用 以及在要顯示的地方放置div標記 代碼:
復制代碼 代碼如下:
<body onLoad="clockon(bgclock)">
<div id="bgclock" class="word_Green"></div>
</body>