时钟显示变颜色

<?xml version="1.0" encoding="windows-1252"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- Created on: 2/25/2008 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>JavaScript Digital Clock</title>
<style>
	body{
		background-color: #95A595;
		color: #354535;
	}
	#clock {
		font-family: "Times New Roman", Georgia, serif; 
		font-size: 100pt;
		background: white;
		padding-left: 5px;
		padding-right: 5px;
		border: 5px solid silver;
		width: 900px;
		margin: 0 auto;
	}
	
</style>
<script type="text/javascript">
		function updateClock()
		{
		  var currentTime = new Date();
		
		  var currentHours = currentTime.getHours();
		  var currentMinutes = currentTime.getMinutes();
		  var currentSeconds = currentTime.getSeconds();
		
		  // Pad the minutes with leading zeros, if required 
			if (currentMinutes < 10)
				currentMinutes = "0" + currentMinutes;
			// The following line is functionally equivalent to the 'if' statement above
      // currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;

		  // Pad the seconds with leading zeros, if required 
		  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
		
		  // Choose either "AM" or "PM" as appropriate
		  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
		
		  // Convert the hours component to 12-hour format
		  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
		
		  // Convert an hours component if "0" to "12"
		  currentHours = ( currentHours == 0 ) ? 12 : currentHours;
		
			// Get hold of the html elements by their ids
			var hoursElement = document.getElementById("hours");
			var minutesElement = document.getElementById("minutes");
			var secondsElement = document.getElementById("seconds");
			var am_pmElement = document.getElementById("am_pm");

			var h = ((currentHours * 255) / 12).toString(16);
			var m = ((currentMinutes * 255) / 60).toString(16);
			var s = ((currentSeconds * 255) / 60).toString(16);
		    //256

			//alert(h);
			hoursElement.style.color = "#" + h.charAt(0)+h.charAt(1)+"0000";    //前面FF
			minutesElement.style.color = "#00" + m.charAt(0) + m.charAt(1) + "00";  //中间FF
			secondsElement.style.color = "#00"  + "00" +s.charAt(0) + s.charAt(1) ;   //后面FF
			am_pmElement.style.color ="#" + "009900";

			// Put the clock sections text into the elements' innerHTML
			hoursElement.innerHTML = currentHours;
			minutesElement.innerHTML = currentMinutes;
			secondsElement.innerHTML = currentSeconds;
			am_pmElement.innerHTML = timeOfDay;



		}

</script>
<script type="text/javascript" src="/d2l/common/mathjax/2.0/MathJax.js?config=MML_HTMLorMML%2c%2fd2l%2flp%2fmath%2fdisplay%2fconfig.js%3fv%3d9.4.1000.126-8" ></script></head>
<!-- The following line calls the updateClock function when the body of this web page loads,
		 then calls the same function every thousand milliseconds interval -->
<body οnlοad="updateClock(); setInterval( 'updateClock()', 1000 )">
		<h1>The JavaScript digital clock</h1>
		
		<div id='clock' style="text-align: center">
		  <span id="hours"></span>:
			<span id='minutes'></span>:
			<span id='seconds'></span>
			<span id='am_pm'></span>
		</div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值