案例:利用JS动态的显示当前时间

<!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>
</head>
<body>
<input type="text" id="showTime"/>
<input type="button" οnclick="clock()" value="getNowTime"/>
<script type="text/javascript">
		function clock(){
			
			//分别获取时分秒
			var date = new Date();
			var hours = date.getHours();
			var minu = date.getMinutes();
			var sed = date.getSeconds();
			
			//检查时间为个位数时,前面加0,如9---09
			hours = check(hours);
			minu = check(minu);
			sed = check(sed);
			
			var strDate = hours + ":" + minu + ":" + sed;
			var textTime = document.getElementById("showTime");
			
			//进行清空
			textTime.value = "";
			textTime.value = strDate;
			
			//计时器,每500ms刷新一次
			window.setTimeout("clock()","500");	
			}
			
		function check(date){
			if (0<=date && date <=9){
				date = "0" + date;
				return date;
				}
			else{
				return date;
				}
			}
</script>
</body>
</html>

 

全部评论

相关推荐

06-23 18:25
沈阳大学 Java
HR已读不回,是我说话方式不对吗?
大白之主:你是串子吗? hr: 我们不招人了,把岗位挂着boss只是因为我闲得慌
点赞 评论 收藏
分享
白火同学:大二有这水平很牛了,可以适当对关键信息加粗一点,比如关键技术、性能指标之类的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务