共享一段Javascript版时钟代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript版时钟</title>
<script>
var oInterval="";
function fnRecycle(){
var oDate=new Date();
var sSwitch="AM";//上午
var iHours=oDate.getHours();
if(iHours>12){
iHours-=12;
sSwitch="PM";//下午
}
var sMinutes=oDate.getMinutes() + "";
if(sMinutes.length==1){
sMinutes="0" + sMinutes;
}
var sSeconds=oDate.getSeconds() + "";
if(sSeconds.length==1){
sSeconds="0" + sSeconds;
}
oTimer.innerHTML=iHours + ":" + sMinutes + ":" + sSeconds + " " + sSwitch;
}
function window_onload() {
if(oInterval==""){
oInterval=window.setInterval("fnRecycle()",1000);
}
}
</script>
</head>
<body language="javascript" onload="return window_onload()">
<!-- 页面调用 -->
Javascript版时钟:<span id="oTimer" ></span>
</body>
</html>
Over!!!
本文介绍了一个使用纯JavaScript实现的网页实时时钟代码示例。该时钟每秒更新一次显示的时间,并采用12小时制显示,同时区分上午(AM)和下午(PM)。通过设置时间间隔来不断获取当前时间并更新页面上的显示。

被折叠的 条评论
为什么被折叠?



