window.onload = function ()
{
var oPan = document.getElementsByTagName("span");
setInterval(getTimes,1000);
getTimes();
function getTimes(){
<span style="color:#ff0000;">var oDate=new Date(); //<span style="font-family: Verdana, Arial, 宋体; line-height: 18px; background-color: rgb(249, 249, 249);">通过 new 关键词来定义 Date 对象 便可以调用这个对象的方法</span></span>
<span style="color:#ff0000;">var oDat=[oDate.getHours(),oDate.getMinutes(),oDate.getSeconds()]; //Date对象方法 getHours()...</span>
<span style="color:#ff0000;">for(var i in oDat) oPan[i].innerHTML=format(oDat[i]);</span>
}
function format(a){
<span style="color:#ff0000;">return a.toString().replace(/^(\d)$/,"0$1");</span>
}
}
简单时钟
for...in... 为一个对象的每个属性或一个数组的每个元素执行一个或多个语句。
// Initialize the array. var arr = new Array("zero","one","two"); // Add a few expando properties to the array. arr["orange"] = "fruit"; arr["carrot"] = "vegetable"; // Iterate over the properties and elements. var s = ""; for (var key in arr) { s += key + ": " + arr[key]; s += "<br />"; } document.write (s); // Output: // 0: zero // 1: one // 2: two // orange: fruit // carrot: vegetable
<span style="color:#ff0000;">return a.toString().replace(/^(\d)$/,"0$1");</span>
a.toString().replace(/^(\d)$/, "0$1"),a.tostring是把传进来的数字转成字符串,然后用字符串的replace方法,这个方法就是对字符串的某一个或多个字符进行替换操作\d匹配数字,^是以什么什么开头,^\d就是匹配一个数字,而且这个数字必须在字符串的开头,\d外面加小括号是为了后面$1的引用,/^(\d)$/, "0$1" 的意思就是把1替换成01