日期:2014-05-16  浏览次数:20464 次

javascript常用对象

1.Date对象:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
<script type="text/javascript">
  var date = new Date();
  document.write(date);
</script>
 </head>
 <body>
 </body>
</html>


运行结果:

我们来改变一下输出的方式:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
<script type="text/javascript">
  var date = new Date();
  document.write(date.getFullYear()+"年"+date.getMonth()+"月"+date.getDate()+"日");
</script>
 </head>
 <body>
 </body>
</html>


运行结果:

明明是9月,却显示的是8月,说明getMonth是从下标0开始的,所以要加1:

 document.write(date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日");


这样就会输出正确的结果了。

 

2.String对象:

2.1

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
<script type="text/javascript">
  var str1 = new String("niujiabin");
  var str2 = "niujiabin";
  alert(str1==str2);
</script>
 </head>
 <body>
 </body>
</html>


结果是true,这里与java是有区别的,并不存在引用,没有equals方法。

 

 

2.2字符串连接

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
<script type="text/javascript">
  var str1 = new String("niujiabin");
  var str2 = "niujiabin";
//  alert(str1==str2);
//字符串的连接
var str3 = str2.concat("maybe","gossip");
alert(str3);
</script>
</hea