简易时钟 Date对象 for...in...语句 replace方法

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值