动态时钟的实现

下面是动态时钟的效果图(ps:不过好像看不出动态效果来):

下面是具体的代码:

<!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>动态时钟</title>
<script type="text/javascript">
//在网页上输出:今天的日期、星期、现在的时间(动态时钟)
function start()
{
	var today=new Date();
	var year=today.getFullYear();
	var month=today.getMonth()+1;
	var day=today.getDate();
	var hours=today.getHours();
	var minutes=today.getMinutes();
	var seconds=today.getSeconds();
	//如果是单位数字,前面补0
	month=month<10? "0"+month :month;
	day=day<10? "0"+day :day;
	hours=hours<10? "0"+hours :hours;
	minutes=minutes<10? "0"+minutes :minutes;
	seconds=seconds<10? "0"+seconds :seconds;
	//时间信息连成字符串
	var str="今天是"+year+"年"+month+"月"+day+"日 "+hours+":"+minutes+":"+seconds;
	//获取id=result的内容
	var obj=document.getElementById("result");
	obj.innerHTML=str;
	//延时器
	window.setTimeout("start()",1000);
}
</script>
</head>

<body onload="start()">
<div id="result"></div>
</body>
</html>

 

posted @ 2016-02-25 12:36  小Q的博客  阅读(1037)  评论(0编辑  收藏  举报