首页 >web前端 >js教程 >html网页写一个时钟

html网页写一个时钟

黄舟
黄舟原创
2017-02-27 14:55:172113浏览

问题?html网页写一个时钟

1.js的形式来写

<!DOCTYPE html>
<html>
<head>
	<title北京时间</title>
	<script type="text/javascript" charset="utf-8" language="javascript">
	function time1(){
	var today = new Date();
	var hour = today.getHours(),
	minute = today.getMinutes(),
	second = today.getSeconds();

	minute = checkTime(minute);
	second = checkTime(second);

	document.getElementById(&#39;txt&#39;).innerHTML=hour+":"+minute+":"+second;
	t = setTimeout(&#39;time1()&#39;,500);
}

function checkTime(i){
	if (i<10) {
		i="0" + i;
	}
  return i;
}
	</script>
</head>
<body onload="time1()">
	<p id="txt"></p>
</body>
</html>


2.jquery的形式写:

<!DOCTYPE html>
<html>
<head>
	<title>时钟</title>
	<link rel="shortcut icon" href="ico/11.ico">
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
	<script src="js/jquery-1.11.3.min.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		$(document).ready(A);
		function A(){
			var today = new Date();
			var h = today.getHours();
			var m = today.getMinutes();
			var s = today.getSeconds();
			m = C(m);
			s = C(s);
			$("#a1").html("当前时间为:"+h+":"+m+":"+s)
			setTimeout(&#39;A()&#39;,500);
		}
		function C(i){
			if(i<10){
				i="0"+i;
			}
			return i;
		}
	</script>
</head>
<body>
	<p id="a1"></p>
</body>
</html>

 以上就是html网页写一个时钟的内容,更多相关内容请关注PHP中文网(www.php.cn)!


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn