个人博客
JavaScript 案例19
Date 获取 时分秒
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Date</title>
</head>
<body>
<script>
/**
* var oDate = new Date()
* getHours() 获取时
* getMinutes() 获取分钟
* getSeconds() 获取秒
*
* */
var oDate = new Date();
alert(oDate.getHours());
alert(oDate.getMinutes());
alert(oDate.getSeconds());
</script>
</body>
</html>JavaScript 案例20
数码时钟
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数码时钟</title>
<style>
body{
font-size: 50px;
background: forestgreen;
}
</style>
</head>
<body>
<div id="clock">
<img src="img/0.png" alt="">
<img src="img/0.png" alt="">:
<img src="img/0.png" alt="">
<img src="img/0.png" alt="">:
<img src="img/0.png" alt="">
<img src="img/0.png" alt="">
</div>
<script>
//补0函数
function toDou(n) {
if (n<10){
return "0"+n;
}else {
return ""+n;
}
}
var oDiv = document.getElementById("clock");
var aImg = oDiv.getElementsByTagName("img");
//var str = "123456";
function clock() {
var oDate = new Date();
var str = toDou(oDate.getHours()) + toDou(oDate.getMinutes())+toDou(oDate.getSeconds());
for(var i = 0; i<aImg.length;i++){
//aImg[i].src = 'img/'+str[i]+'.png';//存在兼容性问题
aImg[i].src = 'img/'+str.charAt(i)+'.png';
}
}
setInterval(clock,1000);
clock();
</script>
</body>
</html>版权声明:本文为canglingyue原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。