一、 在中加入script
pt type="text/javascript">
function gettime(){
var date = new Date();
var hours=0,minutes=0,seconds=0,year=0,mouth=0,day=0;
year=date.getFullYear();
mouth=date.getMonth()+1;
day=date.getDate();
hours=date.getHours();
minutes=date.getMinutes();
seconds=date.getSeconds();
//保证格式,00
mouth=mouth<10?'0'+mouth:mouth;
day=day<10?'0'+day:day;
hours=hours<10?'0'+hours:hours;
minutes=minutes<10?'0'+minutes:minutes;
seconds=seconds<10?'0'+seconds:seconds;
document.getElementById("time").innerHTML=year+'-'+mouth+'-'+day+' '+hours+':'+minutes+':'+seconds;;
window.setTimeout("gettime()",1000);
}
window.οnlοad=gettime;
</script>
二、在body标签加入οnlοad=“gettime()” //gettime()为函数名
<body onload="gettime()">
三、显示
服务器时间: <span class="STYLE7" id='time'> </span>
id的值需要与script对应
本文介绍了一种使用JavaScript实现的实时显示服务器时间的方法。通过在HTML页面中加入特定的script标签,利用JavaScript函数gettime()获取当前时间,并将其格式化为年-月-日 时:分:秒的形式,然后更新页面上指定id的时间元素。同时,通过在body标签中设置onload事件调用gettime()函数,实现页面加载时自动显示并定时刷新时间。

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



