爱学习的站长www.mmic.net.cn

www.mmic.net.cn 欢迎学习共同成长
公告信息
www.mmic.net.cn 欢迎学习共同成长
文章分类
文章档案
文章
JavaScript笔记,JavaScript对象
2011/8/9 13:32:51

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)

 http://pdflist.mmic.net.cn

新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"
 技术   浏览(2486)   评论(0)   关键字
  
Copyright © 2010-2020 power by CYQ.Blog - 秋色园 v2.0 All Rights Reserved