<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
#time{
font-size: 5em;
}
</style>
<script type="text/javascript">
//设置时钟现实格式,个位数前面补零
function geshi(num){
if(num<10){
return '0'+num;
}else{return ''+num;}
}
//获取时间
function gettime(){
now=new Date();
var h=now.getHours();
var m=now.getMinutes();
var s=now.getSeconds();
var time=geshi(h)+':'+geshi(m)+':'+geshi(s);
document.getElementById("time").innerHTML = time;//将时间显示返回id标签内
}
setInterval(function(){ gettime()},1000); //定时1秒钟显示一次,注意格式不要错
</script>
</head>
<body>
<div>
<h1>时钟</h1>
<a id="time"></a>
</div>
</body>
</html>
效果图如下: