JavaScprit---基础代码

     var text = "Hello World";
      document.write("<p>" + text.indexOf("Hello")  + "</p>");
      document.write("<p>" + text.indexOf("World")  + "</p>");
      document.write("<p>" + text.indexOf("world")  + "</p>");
      document.write(text.match("Hello"));
      document.write(text.match("World"));
      document.write(text.match("Supan"));
      document.write(text.replace(/Hello/, "Supans"));  //输出Supans World
      document.write("<br>");
      document.write(text);
      document.write(Date());  //Sat Mar 01 2014 22:58:22 GMT+0800
     
      var d=new Date();   //当前日期时间
      document.write("从 1970/01/01 至今已有:" + d.getTime() + " 毫秒。");
     
      d.setFullYear(1992,10,3);   //设置日期
      document.write(d);





<script type="text/javascript">
  var d = new Date();
  var weekday = new Array(7);
  weekday[0]="星期日";
  weekday[1]="星期一";
  weekday[2]="星期二";
  weekday[3]="星期三";
  weekday[4]="星期四";
  weekday[5]="星期五";
  weekday[6]="星期六";
  document.write("今天是:" + weekday[d.getDay()]);
</script>



数字始终代码:
<html>
<head>
<script type="text/javascript">
function startTime(){
  var today = new Date();
  var h = today.getHours();
  var m = today.getMinutes();
  var s = today.getSeconds();
  //add a zero in front of numbers < 10
  m = checkTime(m);
  s = checkTime(s);
  document.getElementById("txt").innerHTML = h + ":" + m + ":" + s;
  //setTimeout 方法用于在指定的毫秒数后调用函数或计算表达式。
  setTimeout('startTime()',500);
}
  //分钟,秒不足10的前面加上一个字符0
  function checkTime(i){
	  if(i < 10){
		  i = "0" + i;
	  }
		  return i;
  }
</script>
</head>
<body onload="startTime()">
    <div id="txt"></div>
</body>
</html>




数组:
<script type="text/javascript">
     var mycars = new Array();
     mycars[0] = "Saab";
     mycars[1] = "Volvo";
     mycars[2] = "bmw";
     for(var i = 0; i < mycars.length; i++)
   	 {
   	 document.write(mycars[i] + "</br>");
   	 }
     //数组的for循环,x是mycars的下标
     for( x in mycars){
    	 document.write(mycars[x] + "</br>");
     }
</script>/code]
  

你可能感兴趣的:(JavaScript)