初学JavaScript笔记,JavaScript对象

本文摘自www.w3school.com.cn

1.

String-indexof
 1 <html>
2 <body>
3
4 <script type="text/javascript">
5
6 var str="Hello world!"
7 document.write(str.indexOf("Hello") + "<br />")
8 document.write(str.indexOf("World") + "<br />")
9 document.write(str.indexOf("world"))
10
11 </script>
12
13 </body>
14 </html>
15
16
17
18
19 ---------------------------------------------
20 result:
21 0
22 -1
23 6

注意大小写

2.

String-match
 1 <html>
2 <body>
3
4 <script type="text/javascript">
5
6 var str="Hello world!"
7 document.write(str.match("world") + "<br />")
8 document.write(str.match("World") + "<br />")
9 document.write(str.match("worlld") + "<br />")
10 document.write(str.match("world!"))
11
12 </script>
13
14 </body>
15 </html>
16
17
18 -------------------------------------
19 result:
20 world
21 null
22 null
23 world!

3.

String-replace
 1 <html>
2 <body>
3
4 <script type="text/javascript">
5
6 var str="Visit Microsoft!"
7 document.write(str.replace(/Microsoft/,"W3School"))
8
9 </script>
10 </body>
11 </html>
12
13
14
15 --------------------------------------
16 result:
17 Visit W3School!

4.

document.write(Date()) 返回的是当前 星期 月份 日 时间 年

5.

显示时间
 1 <html>
2 <head>
3 <script type="text/javascript">
4 function startTime()
5 {
6 var today=new Date()
7 var h=today.getHours()
8 var m=today.getMinutes()
9 var s=today.getSeconds()
10 // add a zero in front of numbers<10
11 m=checkTime(m)
12 s=checkTime(s)
13 document.getElementById('txt').innerHTML=h+":"+m+":"+s
14 t=setTimeout('startTime()',500)
15 }
16
17 function checkTime(i)
18 {
19 if (i<10)
20 {i="0" + i}
21 return i
22 }
23 </script>
24 </head>
25
26 <body onload="startTime()">
27 <div id="txt"></div>
28 </body>
29 </html>

6.

setFullYear
 1 <html>
2 <body>
3
4 <script type="text/javascript">
5
6 var d = new Date()
7 d.setFullYear(1992,10,3)
8 document.write(d)
9
10 </script>
11
12 </body>
13 </html>
14
15
16
17 ---------------------------------
18 result:
19 Tue Nov 3 21:05:32 UTC+0800 1992
20
21 注意:表示月份的参数介于 011 之间。也就是说,如果希望把月设置为 8 月,则参数应该是 7
22
23
24 将日期对象设置为 5 天后的日期:
25 var myDate=new Date()
26 myDate.setDate(myDate.getDate()+5)

7.

逻辑
<html>
<body>

<script type="text/javascript">
var b1=new Boolean( 0)
var b2=new Boolean(1)
var b3=new Boolean("")
var b4=new Boolean(null)
var b5=new Boolean(NaN)
var b6=new Boolean("false")

document.write(
"0 是逻辑的 "+ b1 +"<br />")
document.write(
"1 是逻辑的 "+ b2 +"<br />")
document.write(
"空字符串是逻辑的 "+ b3 + "<br />")
document.write(
"null 是逻辑的 "+ b4+ "<br />")
document.write(
"NaN 是逻辑的 "+ b5 +"<br />")
document.write(
"字符串 'false' 是逻辑的 "+ b6 +"<br />")
</script>

</body>
</html>


----------------------------------------
0 是逻辑的 false
1 是逻辑的 true
空字符串是逻辑的
false
null 是逻辑的 false
NaN 是逻辑的
false
字符串
'false' 是逻辑的 true
逻辑false
 1 <html>
2 <body>
3
4 <script type="text/javascript">
5 var myBoolean=new Boolean();
6 document.write(myBoolean);
7 document.write("<br />");
8
9 var myBoolean=new Boolean(0);
10 document.write(myBoolean);
11 document.write("<br />");
12
13 var myBoolean=new Boolean(null);
14 document.write(myBoolean);
15 document.write("<br />");
16
17 var myBoolean=new Boolean("");
18 document.write(myBoolean);
19 document.write("<br />");
20
21 var myBoolean=new Boolean(false);
22 document.write(myBoolean);
23 document.write("<br />");
24
25 var myBoolean=new Boolean(NaN);
26 document.write(myBoolean);
27 document.write("<br />");
28 </script>
29
30 </body>
31 </html>

逻辑true
 1 <html>
2 <body>
3
4 <script type="text/javascript">
5 var myBoolean=new Boolean(1);
6 document.write(myBoolean);
7 document.write("<br />");
8
9 var myBoolean=new Boolean(true);
10 document.write(myBoolean);
11 document.write("<br />");
12
13 var myBoolean=new Boolean("true");
14 document.write(myBoolean);
15 document.write("<br />");
16
17 var myBoolean=new Boolean("false");
18 document.write(myBoolean);
19 document.write("<br />");
20
21 var myBoolean=new Boolean("Bill Gates");
22 document.write(myBoolean);
23 document.write("<br />");
24 </script>
25
26 </body>
27 </html>


8.

Math 对象的 floor() 方法和 random() 来返回一个介于 0 和 10 之间的随机数
1 document.write(Math.floor(Math.random()*11)) 

floor是向下取整,random()是0~1之间

9.

RegExp 对象有 3 个方法:test()、exec() 以及 compile()。

test() 方法检索字符串中的指定值。返回值是 true 或 false。

exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。

如果需要找到所有某个字符的所有存在,则可以使用 "g" 参数 ("global")。

RegExp-exec()g
var patt1=new RegExp("e","g");
do
{
result
=patt1.exec("The best things in life are free");
document.write(result);
}
while (result!=null)


------------------------------
result:
eeeeeenull

compile() 方法用于改变 RegExp。既可以改变检索模式,也可以添加或删除第二个参数。

RegExp-compile
 1 var patt1=new RegExp("e");
2
3 document.write(patt1.test("The best things in life are free"));
4
5 patt1.compile("d");
6
7 document.write(patt1.test("The best things in life are free"));
8
9
10 -----------------------------------
11 truefalse

先找"e”,后找"d"

posted @ 2011-08-09 21:30  梦想's技术人员  阅读(188)  评论(0)    收藏  举报