在js中用图片显示当前时间
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div{margin:100px auto;width: 350px;}
</style>
</head>
<body>
<div>
<img src="images/0.png" id="img1">
<img src="images/0.png" id="img2">
<img src="images/second1.png" >
<img src="images/0.png" id="img3">
<img src="images/0.png" id="img4">
<img src="images/second1.png" >
<img src="images/0.png" id="img5">
<img src="images/0.png" id="img6">
</div>
<script type="text/javascript">
//获取图片id
var images1=document.getElementById('img1');
var images2=document.getElementById('img2');
var images3=document.getElementById('img3');
var images4=document.getElementById('img4');
var images5=document.getElementById('img5');
var images6=document.getElementById('img6');
function showTime(){
//获取当前时间
var date=new Date;
var hours=date.getHours();
var minutes=date.getMinutes();
var seconds=date.getSeconds();
//将小时拆分
var q=parseInt(hours/10);
var w=hours%10;
//将图片加入拆分的时间里
images1.src='images'+'/'+q+'.'+'png';
images2.src='images'+'/'+w+'.'+'png';
//拆分分钟
var e=parseInt(minutes/10);
var r=minutes%10;
//将图片加入拆分的时间里
images3.src='images'+'/'+e+'.'+'png';
images4.src='images'+'/'+r+'.'+'png';
//拆分秒
var t=parseInt(seconds/10);
var y=seconds%10;
//将图片加入拆分的时间里
images5.src='images'+'/'+t+'.'+'png';
images6.src='images'+'/'+y+'.'+'png';
}
//调用第一次
showTime();
//每隔一秒调用一次
setInterval(showTime,1000);
</script>
</body>
</html>
网页中所显示: