<!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>
<script>
//window.onload = function(){showTime();}
setInterval("showTime()",1000);
//setTimeout("showTime()",1000);
function showTime(){
var myDate=new Date();
var str = ""+toD(myDate.getHours())+toD(myDate.getMinutes())+toD(myDate.getSeconds());
var aImgs = document.getElementsByTagName("img");
for(var a = 0;a<aImgs.length;a++){
aImgs[a].src="image/"+str.charAt(a)+".png";
}
}
//處理時分秒為一位數時情況
function toD(time){
time = ""+time;
if(time.length<2){
return "0"+time;
}else{
return time;
}
}
</script>
</head>
<body>
<div>
<img src="image/0.png" />
<img src="image/1.png" />
:
<img src="image/0.png" />
<img src="image/2.png" />
:
<img src="image/1.png" />
<img src="image/3.png" />
</div>
</body>
</html>
用到的知識點:
1.document.getElementsByTagName獲取所有的標簽
2.setInterval("todo",1000);//每1秒執行一次
3.setTimeout("todo",1000);//1秒之后執行,只執行一次
4.aImgs[a].src="image/"+str.charAt(a)+".png";//用"="直接給控件設置屬性值
5.window.onload=function(){...}頁面加載后執行