事半功倍系列javascript,二

第七章 使用对象

1.理解对象\属性和方法

1 <body bgcolor="green">

2 <script>

3 document.write("页面背景颜色是:"+document.bgColor)

4 document.write("页面前景颜色是:"+document.fgColor)

5 </script>

2.使用网页元素对象

1 <script>

2 </script>

3 <form name=form1>

4 <textarea name=ta1>dfgfdgfdhfdhdfdfgdf</textarea>

5 <input type=button value="选择文本" onclick=document.form1.ta1.select()>

6 <input type=button value="显示文本" onclick=document.write(document.form1.ta1.value)>

7 </form>

3.使用子对象

1 <form name=form1>

2 <input type=text name=text1 value=hello>

3 </form>

4 <script>

5 document.form1.text1.value="gdfgfd"

6 </script>

7

8 <form name=form1>

9 <input type=radio name=radio1>男

10 <input type=radio name=radio2>女

11 </script>

12 <script>

13 document.form1.radio1.checked=true

14 </script>

4.使用预定义对象

1 <script>

2 str1="dgdfgdfgdfhf固定法固定法功夫攻打法"

3 document.write(str1+"<br>")

4 str2=str1.substr(5)

5 document.write(str2+"<br>")

6 document.write("输出圆的面积:"+Math.PI*Math.pow(5.0,2))

7 </script>

5.创建新对象

1 <script>

2 today=new Date()

3 document.write("今天是"+(today.getMonth()+1)+"月"+today.getDate()+"日"+"<br>")

4 document.write("现在是:"+today.toLocaleString())

5 </script>

6.引用当前对象

1 <form name=form1>

2 <input type=text name=text1 value="dgdgdfgfd" onclick=this.select()>

3 </script>

7.查看对象属性

1 <script>

2 for(prop in window)

3 {document.write("window."+prop+"="+window[prop]+"<br>");}

4 for(prop2 in location)

5 {document.write("location."+prop2+"="+location[prop]+"<br>");}

6 </script>

8.使用Array对象

1 <script>

2 array=new Array(10)

3 array[0]="bark"

4 array[1]="apple"

5 array[2]="nebula"

6 array[3]="cookie"

7 array[4]="technology"

8 document.write("数组元素个数是"+array.Length+"<br>")

9 document.write("用 join将数组合并"+array.join(" ")+"<br>")

10 document.write(" 数组排序"+array.sort())

11 </script>

9.使用 image 对象

1 <img src=**.gif alt="图片提示…." 图片提示是:"+document.images[0].alt+"<br>")

4 document.write("图片边框大小是:"+document.images[0].broder)

5 </script>

10.预加载图像

1 <script>

2 freddy=new Image()

3 freddy.src=freddy.gif

4 </script>

5 <body onload=document.images[0].src=freddy.src>

6 ,<img src="blank.gif">

7 </body>

11.改变图像

1 <img src=freddy.gif><br>

2 <form name=form1>

3 <input type=button name=button1 value="改变图像" onclickd=document.images[0].src=dudjp.gif>

4 </form>

12.使用link和anchor对象

1 <a name=anchor1>锚点1<br>

2 <a href=http://www.microsoft.com>Microsoft</a><br>

3 <a href=http://www.sohu.com>sohu</a><br>

4 <a href=http://www.sina.com.cn>sina</a><br>

5 <script>

6 document.write("本页面共有"+document.links.length+"链接"+"<br>")

7 document.write("本页面共有"+document.anchors.length+"锚点"+"<br>")

8 document.write("第一个链接协议是"+document.links[0].protocol+"<br>")

9 document.write("第一个链接路径是"+document.links[0].pathnamel+"<br>")

10 document.write("第一个链接href是"+document.links[0].hrefl+"<br>")

11 </script>

13.改变链接

1 <a href =http://www.microsoft.com>link</a>

2 <form name=form1>

3 <input type=button name=button1 value="改变链接" onclick=document.links[0].href='http://www.sohu.com'>

4 </form>

14.使用history对象

1 <form name=form1>

2 <input type=button name=button1 value="向后返回2页" onclick=window.history.go(-2)>

3 </form>

第八章 使用窗口

1.在浏览器的状态栏上显示文本

1 <body onload=window.status="欢迎光临我的站点">

2 <a href=http://www.sohu.com>sohu</a>

3 </body>

2.改变背景色

1 <script>

2 document.bgColor="orange"

3 </script>

3.列举背景颜色

1 <body bgColor =green>

2 <script>

3 document.write("当前背景色是:"+document.bgColor)

4 </script>

5 </body>

4.改变文本和链接颜色

1 <script>

2 document.bgColor="orange"

3 document.fgColor="blue"

4 document.linkColor="red"

5 </script>

6 <h2>看看这段文本颜色</h2>

7 <a href=http://www.sohu.com>sohu</a>

8 </body>

5.改变文档标题

1 <script>

2 name="Mouse"

3 document.title="welcome to "+name+"'s House"

4 document.write(document.title)

5 </script>

6.显示修改日期

1 <script>

2 document.write("本页面最后修改时间是"+document.lastModified)

3 </script>

7.查看当前文档的URL

1 <script>

2 document.write("本页面的URL:"+document.URL)

3 </script>

8.查看引用页

1 <script>

2 document.write("本页面的引用页是"+document.referrer)

3 </script>

9.打开新的浏览器窗口

1 <script>

2 window.open("*.htm","title","width=200,height=400,resizable=yes")

3 </script>

10.关闭远程窗口

1 close.html:

2 <script>

3 document.write("正文")

4 </script>

5 <form name=form1>

6 <input type=button name=button1value="关闭" onclick=window.close()>

7 </form>

8

9 open.html

10 <script>

11 window.open("close.html","romote","width=200,height=400,resizable=yes")

12 </script>

13

11.打印窗口

1 <script>

2 document.write("正文")

3 </script>

4 <form name=form1>

5 <input type=button value=打印 onclick=window.print()>

6 </form>

12.移动窗口

1 <form name=form1>

2 水平方向<input type=text name=x value=20>

3 垂直方向<input type=text name=y value=50>

4 <input type=button value="移动窗口到…"onclick=window.moveTo(document.form1.x.value,document.form1.y.value)>

5 </form>

6

7 <form name=form1>

8 水平方向<input type=text name=x value=20>

9 垂直方向<input type=text name=y value=50>

10 <input type=button value="移动窗口"onclick=window.moveBy(document.form1.x.value,document.form1.y.value)>

11 </form>

13.改变窗口大小

1 <form name=form1>

2 水平方向<input type=text name=x value=200>

3 垂直方向<input type=text name=y value=500>

4 <input type=button value="改变窗口大小到….."onclick=window.resizeTo(document.form1.x.value,document.form1.y.value)>

5 </form>

6

7 <form name=form1>

8 水平方向<input type=text name=x value=200>

9 垂直方向<input type=text name=y value=500>

10 <input type=button value="改变窗口大小"onclick=window.resizeBy(document.form1.x.value,document.form1.y.value)>

11 </form>

14.用警告对话框通知用户

1 <script>

2 window.alert("welcome")

3 </script>

15.用提示对话框接受输入

1 <script>

2 name=window.prompt("输入姓名","姓名")

3 document.write(" 欢迎您:"+name+"来到这里")

4 </script>

16.用确认对话框使用户做出决定

1 <script>

2 like=window.confirm("你觉得好吗?")

3 if(like==true)

4 document.write("谢谢你的夸奖")

5 else

6 document.write("希望得到你的夸奖")

7 </script>

第九章 使用字符串

1.使用字符串对象

1 <script>

2 mystring="gdgdfgfddddaaaaaaaaaaaabbbbbbbbbbbbbbbbbvbhg.<br>"

3 document.write(mystring)

4 document.write(mystring.bold())

5 document.write(mystring.toUpperCase())

6 </script>

2.使用子字符串

1 <script>

2 str1="fdsf 1111 gfdgfd dfdsf cccc dddd.<br>"

3 document.write(str1)

4 document.write(str1.substring(0,13)+"<br>")

5 document.write(str1.substr (20,11)+"<br>")

6 </script>

3.连接字符串

1 <script>

2 str1="may you find"

3 str2="peace,happiness and prosperity.<br>"

4 document.write(str1+"<br>")

5 document.write(str2)

6 document.write(str1.concat(str2))

7 document.write(str1+=str2)

8 </script>

4.格式化字符串变量

1 <script>

2 str1="peace,happiness and prosperity.<br>"

3 document.write(str1)

4 document.write(str1.big())

5 document.write(str1.small())

6 document.write(str1.bold())

7 document.write(str1.italics())

8 document.write(str1.strike())

9 document.write(str1.fontsize(6))

10 document.write(str1.fontcolor(green))

11 </script>

5.创建锚和链接

1 <script>

2 str1="this is the bigginning of the page.<br>"

3 str2="….<br>"

4 str3="this is the end of the page .<br>"

5 str4="link to the start<br>"

6 str5="link to the end<br>"

7 document.write(str1.anchor("start"))

8 for(i=0;i<10;i++)

9 document.write(str2);

10 document.write(str3.anchor("end"))

11 document.write(str4.link("#start"))

12 document.write(str5.link("#end"))

13 </script>

6.确定字符串长度

1 <script>

2 str1="this is the bigginning of the page."

3 document.write(str1+"<br>")

4 document.write( "字符串的长度是:"+str1.length)

5 document.write("字符串全部大写是;"+str1.toUpperCase())

6 document.write("字符串全部小写是;"+str1.toLowerCase())

7 </script>

7.在字符串内搜索

1 <script>

2 str1="this is the end of the line.<br>"

3 document.write(str1)

4 document.write("字符end在字符串的位置是"+str1.search("end"))

5 document.write("字符dog在字符串的位置是"+str1.search("dog"))

6 </script>

8.定位字符串中的字符

1 <script>

2 str1="spring is a time for flowers and trees and baby bunnles<br>"

3 document.write(str1)

4 document.write("the index for the second word ‘and' is"+str1.indexOf("and",30))

5 documednt.write("the last index of the word ‘and' is "+str1.lastIndexOf("and"))

6 </script>

9.替换字符串中的文本

1 <script>

2 str1="spring is a time for flowers and trees and baby bunnles<br>"

3 document.write(str1)

4 document .write(str1.replace("and",","))

5 </script>

10.字符串分离

1 <script>

2 str1="spring is a time for flowers and trees and baby bunnles<br>"

3 document.write(str1)

4 str1array=str1.split(" ")

5 document.write(str1array[0]+"<br>")

6 document.write(str1array[1]+"<br>")

7 document.write(str1array[2]+"<br>")

8 document.write(str1array[3]+"<br>")

9 </script>

第十章 使用日期和时间

1.使用Date对象

1 <script>

2 cdate=new Date("august 2,1989 12:30:00")

3 document.write(cdate)

4 </script>

2.显示当地时间和日期

1 <script>

2 cdate=new Date()

3 document.write("当前时间是:"+cdate.toGMTString()+"<br>")

4 document.write("日期和时间是:"+cdate.toLocaleString())

5 </script>

3.获得时间和日期值

1 <script>

2 cdate=new Date()

3 document.write("显示当前的星期"+cdate.getDay()+"<br>")

4 document.write("显示当前的月份"+cdate.getMonth()+"<br>")

5 document.write("显示当前的日期"+cdate.getDay()+"<br>")

6 document.write("显示当前的年份"+cdate.getYear()+"<br>")

7 document.write("显示当前的小时"+cdate.getHours()+"<br>")

8 document.write("显示当前的分钟"+cdate.getMinutes()+"<br>")

9 document.write("显示当前的秒"+cdate.getSeconds()+"<br>")

10 </script>

4.设置时间和日期值

1 <script language=javascript>

2 cdate=new Date("December 25,1984")

3 document.write("显示日期"+cdate+"<br>")

4 document.write("设置月份"+cdate.setMonth(10)+"<br>")

5 document.write("设置日期"+cdate.setDate(23)+"<br>")

6 document.write("设置年份"+cdate.setYear(2000)+"<br>")

7 document.write("设置小时"+cdate.setHours(13)+"<br>");

8 document.write("设置分钟"+cdate.setMinutes(47)+"<br>");

9 document.write("设置秒"+cdate.setSeconds(23)+"<br>");

10 document.write("显示设置后的日期和时间"+cdate);

11 </script>

第十一章 使用Math对象

1. 使用Math对象

1 <script language=javascript>

2 </script>

3 <form name=form1>

4 圆的半径:<input type=text name=rad><br>

5 圆的面积:<input type=text name=area><br>

6 <input type=button name=button1 value=计算圆的面积 onclick=document.form1.area.value=document.form1.rad.value*document.

7 form1.rad.value*Math.PI>

8 </form>

2.生成随机数

1 <script>

2 array1=new Array(

3 "这是第1句",

4 "这是第2句",

5 "这是第3句",

6 "这是第4句",

7 "这是第5句",

8 "这是第6句")

9 RandomNo=Math.floor(array1.length*Math.random())

10 document.write("随机输出某一句"+"<br>"+array1[RandomNo])

11 </script>

3.使用平方根

1 <form name=form1>

2 value:<input type=text name=va1><br>

3 平方根<input type=text name=sqrt><br>

4 <input type=button name=button1 value=计算平方根

5 onclick="document.form1.sqrt.value=Math.sqrt(document.form1.va1.value)">

6 </form>

4.数字的舍入

1 <form name=form1>

2 输入<input type=text name=val><br>

3 舍入的结果<input type=text name=round><br>

4 <input type=button name=button1 value=计算结果 onclick=document.form1.round.value=Math.round(document.form1.val.value)>

5 </form>

5.乘方运算

1 <form name=form1>

2 底数<input type=text name=val><br>

3 指数<input type=text name=power><br>

4 幂<input type=text name=result><br>

5 <input type=button name=button1 value=计算结果 onclick="document.form1.result.value=Math.pow (document.form1.val.value,document.form1.power.value)">

6 </form>

6.发现最小值和最大值

1 <form name=form1>

2 数字1<input type=text name=val1><br>

3 数字2<input type=text name=val2><br>

4 最小值<input type=text name=min><br>

5 最大值<input type=text name=max><br>

6 数字1<input type=button value=计算 onclick="document.form1.min.value=Math.min (document.form1.val1.value,document.form1.val2.value);document.form1.

7 max.value= Math.max(document.form1.val1.value,document.form1.val2.value)">

8 </form>

第十二章 使用表单

1.使用文本框

1 <form name=form1>

2 <input type=text value="information ,please"name=text1>

3 </form>

4 <script>

5 document.write("表单text1类型是: "+document.form1.text1.type+"<br>")

6 document.write("表单text1名称是: "+document.form1.text1.name+"<br>")

7 document.write("表单text1值是: "+document.form1.text1.value+"<br>")

8 document.write("表单text1大小是: "+document.form1.text1.size+"<br>")

9 </script>

10

11 <form name=form1>

12 <input type=text name=text1 value=click here

13 onfocus=document.form1.text1.select()>

14 </form>

15

2.使用密码框

1 <form name=form1>

2 <input type=password name=pw1 value=daylight>

3 </form>

4 <script>

5 document.write("表单pw1的类型:"+document.form1.pw1.type+"<br>")

6 document.write("表单pw1的名称:"+document.form1.pw1.name+"<br>")

7 document.write("表单pw1的值:"+document.form1.pw1.value+"<br>")

8 document.write("表单pw1的大小:"+document.form1.pw1.size+"<br>")

9 </script>

3.使用隐藏字段

1 <form name=form1>

2 <input type=hidden name=hid1 value=piece of eight>

3 </form>

4 <script>

5 document.write("表单hid1的类型:"+document.form1.hid1.type+"<br>")

6 document.write("表单hid1的名称:"+document.form1.hid1.name+"<br>")

7 document.write("表单hid1的值:"+document.form1.hid1.value+"<br>")

8 </script>

4.使用文本区域框

1 <form name=form1>

2 <textarea name=ta1>how many grains of sand are there in the sahara desert?</textarea>

3 </form>

4 <script>

5 document.write("表单ta1的类型:"+document.form1.ta1.type+"<br>")

6 document.write("表单ta1的名称:"+document.form1.ta1.name+"<br>")

7 document.write("表单ta1的值:"+document.form1.ta1.value+"<br>")

8 document.write("表单ta1的横向宽度:"+document.form1.ta1.cols+"<br>")

9 document.write("表单ta1的纵向宽度:"+document.form1.rows.value+"<br>")

10 </script>

6.使用重置按钮

1 <form name=form1>

2 <input type=reset name=reset1 value="rest form">

3 </form>

4 <script>

5 document.write("表单reset1的类型:"+document.form1.reset1.type+"<br>")

6 document.write("表单reset1的名称:"+document.form1.reset1.name+"<br>")

7 document.write("表单reset1的值:"+document.form1.reset1.value+"<br>")

8 </script>

7.使用提交按钮

1 <form name=form1>

2 <input type=submit name=submit1 value="submit form">

3 </form>

4 <script>

5 document.write("表单submit1的类型:"+document.form1.submit1.type+"<br>")

6 document.write("表单submit1的名称:"+document.form1.submit1.name+"<br>")

7 document.write("表单submit1的值:"+document.form1.submit1.value+"<br>")

8 </script>

8.使用复选按钮

1 <form name=form1>

2 <input type=checkbox name=cb1 >computer savvy?

3 </form>

4 <script>

5 document.write("表单cb1的类型:"+document.form1.cb1.type+"<br>")

6 document.write("表单cb1是否被选择?:"+document.form1.cb1.checked+"<br>")

7 document.write("表单cb1的名称:"+document.form1.cb1.name+"<br>")

8 </script>

9.使用单选按钮

1 <form name=form1>

2 <input type=radio name=radio1>male

3 <input type=radio name=radio1>female

4 </form>

5 <script>

6 document.write("第一个按钮被选择"+document.form1.radio1[0].checked+"<br>")

7 document.write("第二个按钮被选择"+document.form1.radio1[1].checked+"<br>")

8 document.write("按钮的名称"+ document.form1.radio1[0].name+"<br>")

9 document.write("按钮的个数"+document.form1.radio1.length)

10 </script>

10.使用选择列表

1 <form name=form1>

2 <select name=select1 size=4>

3 <option name=option1 value=lon>london,England</option>

4 <option name=option2 value=dub>Dublin,Ireland</option>

5 </select>

6 </form>

7 <script>

8 document.write("这个选择列表的名称"+document.form1.select1.name+"<br>")

9 document.write("这个选择列表的长度"+document.form1.select1.length+"<br>")

10 document.write("这个选择列表当前被选择的索引号"+document.form1.select1.selectedIndex+"<br>")

11 document.write("这个选择列表的尺寸"+document.form1.select1.size+"<br>")

12 </script>

11.验证表单的有效性

1 <script>

2 function validate(){

3 if(document.form1.text1.value!='1'||'2'||'3'||'4'){

4 alert("请输入1~4的整数")

5 }

6 }

7 </script>

8 <form name=form1>

9 请输入1~4的整数:

10 <input type=text name=text1 size=4 onchange=validate()>

11 </form>

12.控制表单焦点

1 <form name=form1>

2 <input type=text name=text1 value=where is you focus?><br>

3 <input type=text name=text2 value=is there?><br>

4 <input type=text name=text3 value=or maybe here?><br>

5 <input type=button name=button1 value="text box #1" onclick=document.form1.text1.focus()><br>

6 <input type=button name=button2 value="text box #2" onclick=document.form1.text2.focus()><br>

7 <input type=button name=button3 value="text box #3" onclick=document.form1.text3.focus()><br>

8 </form>

第十四章 使用navigator

1.使用navigator对象

1 <script>

2 document.write("navigator对象的属性"+"<br>")

3 document.write("appcodename:"+navigator.appCodeName+"<br>")

4 document.write("appname::"+navigator.appName+"<br>")

5 document.write("appversion:"+navigator.appVersion+"<br>")

6 document.write("platform:"+navigator.platform+"<br>")

7 document.write("userAgent:"+navigator.userAgent+"<br>")

8 </script>

9 <script>

10 document.write("navigator对象的方法"+"<br>")

11 document.write("javaEnabled():"+navigator.javaEnabled())

12 </script>

2.检查用户的浏览器

1 <script>

2 if(navigator.appName.indexOf("Microsoft")!=-1){

3 document.write("用户浏览器是微软的IE浏览器"+"<br>")}

4 else if(navigator.appName.indexOf("Netscape")!=-1){

5 document.write("用户浏览器是netscape的netscape浏览器"+"<br>")}

6 if(navigator.appVersion.indexOf("4.0")!=-1){

7 document.write("you are using a version 4.0compatible browser")

8 }

9 else{

10 document.write("this browser is not 4.0 compliant")}

11 </script>

3.检测用户的操作系统

1 <script>

2 if (navigator.platform.indexOf("win32")!=-1){

3 document.write("you are using a computer running windows 95 or highter")}

4 else{

5 document.write("this computer is not running windows 95 or higher")}

6 </script>

4.使用location对象

<script>

document.write("location对象的属性"+"<br>")

document.write("hash"+location.hash+"<br>")

document.write("hostname"+location.hostname+"<br>")

document.write("host"+location.host+"<br>")

document.write("href"+location.href+"<br>")

document.write("port"+location.port+"<br>")

document.write("search"+location.search+"<br>")

</script>

重新加载网页

<form name=form1>

<input type=button name=button1 value=重新加载本页 onclick=location.reload>

</form>

5.使用cookie

1 <script>

2 finction makecookie(){

3 if(!document.cookie){

4 name=prompt("请输入你的姓名");

5 document.cookie="name="+name+";";}

6 }

7 </script>

8

9 <body onload=makecookie()>

10 <script>

11 function makecookie(){

12 if(!document.cookie){

13 name=prompt("请输入你的姓名")

14 document.cookie="name="+name+";";

15 namestart=document.cookie.indexOf("=");

16 nameend=document.cookieindexOf(";");

17 document.writeln("your name is:"+document.cookie.substring(namestart+1,nameend)+",br>")

18 }

19 }

20 </script>

21