距离某天还有 天
这次主要测试了几个主要的对象,包括string,date,数组。用date对象做了个类似倒计时的功能,呵呵
<!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>javascript对象</title>
<script language="javascript" type="text/javascript">
var regExp=/\s\*/g;
/*---------------string对象------------------*/
var str="this *is *test *string";
var sTxt=str.charAt(3);
var aTxt=str.charCodeAt(3);
var btxt=str.indexOf("s");
var cTxt=str.slice(0,4);
var dTxt=str.split(" ");
var subTxt=str.substr(0,4);
var regTxt=str.match(regExp);
var regSearchTxt=str.search(regExp);
document.writeln("原始字符串:"+str+"<br />");
document.writeln("charAt(3),返回索引字符:"+sTxt+"<br />");
document.writeln("charCodeAt(3),返回unicode字符代码:"+aTxt+"<br />");
document.writeln("indexOf,返回指定字符索引"+btxt+"<br />");
document.writeln("slice(0,4),返回0,4之间的字符:"+cTxt+"<br />");
document.writeln("split(' '),以空格截取字符成数组:"+dTxt+"<br />");
document.writeln("substr(0,4),截取两个索引之间的字符:"+subTxt+"<br />");
document.writeln("match,返回匹配的字符串:"+regTxt+"<br />");
document.writeln("search,返回匹配的字符串的便宜位置:"+regSearchTxt+"<br />");
/*-----------------正则表达式---------------*/
//test
var result_tst=regExp.test(str);
document.writeln("regExp.test(str),返回是否匹配:"+result_tst+"<br />");
//exec
var result_exec=regExp.exec(str);
document.writeln("返回匹配的内容:"+result_exec+"<br />");
var result=str.replace(regExp,"_");
document.writeln("返回替换的内容:"+result+"<br/>");
//匹配规则
var reg2=/:\s*\w*\s*[0-9]+\s[0-9]{4}/;
var reg1=/:\D*\s*\d+\s\d+/;
var reg3=/w*@w+.w/;
var str2="this is a date: march 12 2010";
var eTxt=str2.match(reg2);
document.writeln(eTxt+"<br/ >");
//*----------------Date对象--------------------*/
var now=new Date();
var hours=now.getHours()+7*24;
now.setHours(hours);
document.write("一个星期后是:"+now+"<br/>");
document.write("现在是"+now.getFullYear()
+"年"+(now.getMonth()+1)+"月"+now.getDate()
+"日"+now.getHours()+"时"+now.getMinutes()
+"分"+now.getSeconds()+"秒"+"<br/>");
//计时器
function countTime(){
var targetTime = new Date(2010,6,24,12,0,0,0);
var nowTime = new Date();
//距离目标时间
var distanTime=targetTime.getTime()-nowTime.getTime();
days = Math.floor( distanTime / 1000 / 60 / 60 / 24);
hours = Math.floor(distanTime/ 1000 / 60 / 60 - (24 * days));
minutes = Math.floor( distanTime/1000/60-(24*60*days)-(60*hours));
seconds =distanTime/1000-(24*60*60*days)-(60*60*hours)-(60*minutes);
secondsRound = Math.round(seconds);
document.getElementById("days").innerHTML="<font color=red>"+days+"</font>";
document.getElementById("time").innerHTML=hours+":"+minutes+":"+secondsRound;
}
setInterval("countTime()",250);
//数组
var firstArr=new Array("one","two","three");
var secondArr=["one","two","three"];
var remove=firstArr.slice(2,3);
var splicArr=secondArr.splice(1,1);
document.writeln(remove+"<br/>");
document.writeln(splicArr+"<br/>");
//队列
var fifoArr=new Array();
fifoArr.push("apple");
fifoArr.push("banana");
var length=fifoArr.push("cherry");
document.writeln("数组的长度是:"+length+" 数组内容是:"+fifoArr+"<br />");
fifoArr.shift();
document.writeln("使用shift命令删除第一个元素:"+fifoArr+"<br />");
fifoArr.unshift("addFruite");
document.writeln("使用unshift命令增加第一个元素:"+fifoArr+"<br />");
fifoArr.pop();
document.writeln("使用pop命令最后一个元素:"+fifoArr+"<br />");
</script>
</head>
<body>
<span>距离某天还有</span><span id="days"></span>天<span id="time"></span>
</body>
</html>