---
title: "事半功倍系列 javascript"
author: "249080378"
date: "2010-03-30T16:10:50Z"
tags: []
categories: ["编程语言"]
slug: "javascript"
draft: false
lastmod: "2010-03-30T16:10:50Z"
id: 628323
url: "https://249080378.iteye.com/blog/628323"
---

第一章javascript简介

1.在地址栏输入javascript语句

Javascript:Document.write("显示文字")

2.将javascript嵌入 HTML文档

&lt;script language=javascript&gt;  
document.bgColor="blue"  
&lt;/script&gt;

第二章 使用变量和数组

1.声明变量

&lt;script language=javascripe&gt;  
Var answer1,answer2,answer3,answer4;  
answer1=9;  
answer2=2.5  
answer3="Milkey May"  
answer4=true  
&lt;/script&gt;

2.使用整数

&lt;script language=javascript&gt;  
var decimalNum,hexadecimalNum,octalNum  
decimalNum=24  
hexadecimalNum=0x24  
octalNum=024  
document.write("显示十进制数："+ decimalNum+"&lt;br&gt;")  
document.write("显示十六进制数："+ hexadecimalNum +"&lt;br&gt;")  
document.write("显示八进制数："+ octalNum +"&lt;br&gt;")  
&lt;/script&gt;

3.使用浮点数

&lt;script language=javascript&gt;  
var num1,num2,num3,num4  
num1=1234567890000.0  
num2=5.14e23  
num3=0.0000123456  
num4=6.0254e3-4  
document.write("浮点数1："+num1+"&lt;br&gt;")  
document.write("浮点数2："+num2+"&lt;br&gt;")  
document.write("浮点数3："+num3+"&lt;br&gt;")  
document.write("浮点数4："+num4+"&lt;br&gt;")  
&lt;/script&gt;

4.使用布尔值

&lt;script language=javascript&gt;  
var answer1,answer2  
answer1=true  
answer2=false  
document.write("显示布尔1："+answer1+"&lt;br&gt;")  
document.write("显示布尔2："+answer2+"&lt;br&gt;")  
&lt;/script&gt;

5.使用字符串

&lt;script language=javascript&gt;  
var str1,str2  
str1="fdsgdg dsfdsf china"  
str2="武汉市广播电视大学"  
document.write("显示字符串1："+str1+"&lt;br&gt;")  
document.write("显示字符串2："+str2+"&lt;br&gt;")  
&lt;/script&gt;

6.确定变量类型

&lt;script&gt;  
var answer1,answer2,answer3,answer4  
answer1=9  
answer2=2.5  
answer3="milky may"  
answer4=true  
document.write("变量1的类型是："+typeof answer1 +"&lt;br&gt;")  
document.write("变量2的类型是："+typeof answer2 +"&lt;br&gt;")  
document.write("变量3的类型是："+typeof answer3 +"&lt;br&gt;")  
document.write("变量4的类型是："+typeof answer4 +"&lt;br&gt;")  
&lt;/script&gt;

7.将字符串转换成数字

&lt;script&gt;  
var str1="31 days in january"  
var int1=parseInt(str1)  
document.write("str1的数据类型是 ："+typeof str1+"&lt;br&gt;")  
document.write("int1的数据类型是 ："+typeof int1+"&lt;br&gt;")  
&lt;/script&gt;

8.将数字转换成字符串

&lt;script&gt;  
var int1=256  
var str1=""+int1  
document.write("str1的数据类型是 ："+typeof str1+"&lt;br&gt;")  
document.write("int1的数据类型是 ："+typeof int1+"&lt;br&gt;")  
&lt;/script&gt;

9.声明数组

&lt;script&gt;  
array=new Array(5)  
array\[0]=1  
array\[1]=3  
array\[2]=5  
array\[3]=7  
array\[4]=11  
document.write("数组是："+array\[0]+" "+array\[1]+" "+array\[2]+" "+array\[3]+" "+array\[4])  
&lt;/script&gt;

10.确定数组元素的个数

&lt;script&gt;  
array=new Array(5)  
array\[0]=1  
array\[1]=3  
array\[2]=5  
array\[3]=7  
array\[4]=11  
document.write("数组是："+array\[0]+" "+array\[1]+" "+array\[2]+" "+array\[3]+" "+array\[4]+"&lt;br&gt;")  
document.write("数组的元素个数是"+array.length)  
&lt;/script&gt;

11.将数组转换为字符串

&lt;script&gt;  
array=new Array()  
array\[0]="dark"  
array\[1]="apple"  
array\[2]="nebula"  
array\[3]="water"  
str1=array.join()  
str2=array.join(" ")  
document.write(str1+"&lt;br&gt;")  
document.write(str2)  
&lt;/script&gt;

12.对数组排序

&lt;script&gt;  
array=new Array()  
array\[0]="dark"  
array\[1]="apple"  
array\[2]="nebula"  
array\[3]="water"  
str1=array.sort()  
document.write(str1+"&lt;br&gt;")  
&lt;/script&gt;

第三章 创建表达式

1.使用算术运算符

&lt;script&gt;  
var1=12  
var2=10  
varadd=var1+var2  
varsub=var1-var2  
varmult=var1\*var2  
vardiv=var1/var2  
varmod=var1%var2  
document.write("数据1是："+var1+"&lt;br&gt;")  
document.write("数据2是："+var2+"&lt;br&gt;")  
document.write("数据相加是："+varadd+"&lt;br&gt;")  
document.write("数据相减是："+varsub+"&lt;br&gt;")  
document.write("数据相乘是："+varmult+"&lt;br&gt;")  
document.write("数据相除是："+vardiv+"&lt;br&gt;")  
document.write("数据相除取余数是："+varmod+"&lt;br&gt;")  
&lt;/script&gt;

2.递增变量和递减变量

&lt;script&gt;  
days=1  
document.write("输出变量"+days+"&lt;br&gt;")  
days++  
document.write("递增后变量变为："+days)  
&lt;/script&gt;

3.创建比较表达式

&lt;script&gt;  
daysofmonth=28  
if(daysofmonth==28)  
month="february"  
document.write("days of month:"+daysofmonth+"&lt;br&gt;")  
document.write("month:"+month)  
&lt;/script&gt;

4.创建逻辑表达式

&lt;script&gt;  
dayofmonth=28  
if(dayofmonth==28 || dayofmonth==29)  
month="february"  
document.write("days of month:"+dayofmonth+"&lt;br&gt;")  
document.write("month:"+month)  
&lt;/script&gt;

5.使用条件运算符

&lt;script language="javascript"&gt;  
stomach="hungry";  
time="5:00";  
(stomach=="hungry"&&time=="5:00") ? eat = "dinner":eat="a snack";  
document.write("输出结果"+eat);  
&lt;/script&gt;

6.识别数字

&lt;script&gt;  
var1=24;  
(isNaN(var1))?document.write("变量var1"+var1+"不是数字"):Document.write("变量var1"+var1+"是数字")  
&lt;/script&gt;

第四章 控制程序流程

1.使用IF –Else语句

&lt;script&gt;  
month="december"  
date=25  
if(month=="december" && date==25)  
document.write("今天是圣诞节,商店关门")  
else  
document.write("欢迎,您来商店购物")  
&lt;/script&gt;

2.使用for 循环

&lt;script&gt;  
for (count=1;count&lt;=10;count++)  
document.write("输出第"+count+"句"+"&lt;br&gt;")  
&lt;/script&gt;

3.使用while循环

&lt;script&gt;  
count=1  
while(count&lt;=15){  
document.write("输出第"+count+"句" +"&lt;br&gt;")  
count++}  
&lt;/script&gt;

4.中断循环

&lt;script&gt;  
count=1  
while(count&lt;=15){  
count++  
if(count==8)  
break;  
document.write("输出第"+count+"句"+"&lt;br&gt;")}  
&lt;/script&gt;

5.继续循环

&lt;script&gt;  
count=1  
while(count&lt;=15){  
count++  
if(count==8)  
continue;  
document.write("输出第"+count+"句"+"&lt;br&gt;")}  
&lt;/script&gt;

6.使用javascript定时器

&lt;script&gt;  
function rabbit()  
{document.write("输出语句")  
}  
&lt;/script&gt;  
&lt;body onload=window.setTimeout(rabbit(),5000)&gt;

7.设置定期间隔

&lt;script&gt;  
window.setInterval("document.form1.text2.value=document.form1.text1.value",3000)  
&lt;/script&gt;  
&lt;form name=form1&gt;  
&lt;input type=text name=text1&gt;&lt;br&gt;  
&lt;input type=text name=text2&gt;&lt;br&gt;  
&lt;/form&gt;

8.清除超时和间隔

&lt;script&gt;  
stop=window.setInterval("document.form1.text2.value=document.form1.text1.value",300)  
&lt;/script&gt;  
&lt;form name=form1&gt;  
&lt;input type=text name=text1&gt;&lt;br&gt;  
&lt;input type=text name=text2&gt;&lt;br&gt;  
&lt;input type=button name=button1 value=" 清除超时和间隔" onclick=clearInterval(stop)&gt;  
&lt;/form&gt;

第五章 使用函数

1.声明函数

&lt;script&gt;  
function quote()  
{ document.write("输出语句")  
}  
&lt;/script&gt;

2.调用函数

&lt;script&gt;  
function quote()  
{ document.write("输出语句")  
}  
quote()  
&lt;/script&gt;

3.了解全局变量和局部变量

任何不用 var关键字声明的变量都是全局变量,任何在函数外声明的变量都是全局变量

4.将参数传送给函数

&lt;script&gt;  
function f(item)  
{document.write("输出参数"+item+"&lt;br&gt;")  
}  
f("fgdfgd")  
f("参数二")  
&lt;/script&gt;

5.从函数返回值

&lt;script&gt;  
function average(var1,var2,var3)  
{ave=(var1+var2+var3)/3;  
document.write("输出结果");  
return ave;  
}  
document.write(average(34,56,78))  
&lt;/script&gt;

6.通过HTML链接调用函数

&lt;script&gt;  
function quote(){  
document.write(" 输出字符串")  
}  
&lt;/script&gt;  
&lt;a href=javascript:quote()&gt;通过HTML链接调用函数&lt;/a&gt;  
&lt;a href=javascript:Document.write("输出字符")&gt; 通过HTML链接调用函数,直接写javascript语句&lt;/a&gt;

第六章 处理事件

1.检查鼠标单击

&lt;form name=form1&gt;  
&lt;input type=button name=button1 value=hello onclick=document.form1.button1.value='there'&gt;  
&lt;/form&gt;

2.检测双击

&lt;form name=form1&gt;  
&lt;input type=button name=button1 value=hello onclick=document.form1.button1.value='你单击了按钮' ondblclick=document.form1.button1.value='你双击了该按钮'&gt;  
&lt;/form&gt;

3.创建悬停按钮

&lt;img src=go.gif onmouseover=document.images\[0].src='go2.gif' onmouseout= document.images\[0].src='go.gif'&gt;

4.检测按键

&lt;form name=form1&gt;  
&lt;input type=text name=text1 value=hello onkeypress="if(window.event.keyCode=='100') document.form1.text1.value='你按了d键'"&gt;  
&lt;/form&gt;

5.设置焦点

&lt;form name=form1&gt;  
&lt;input type=text name=text1 value=hello  
onfous=document.form1.text1.value='该文本框获得焦点'  
onblur=document.form1.text1.value='该文本框失去焦点'&gt;  
&lt;/form&gt;

6.检测下拉菜单选择

&lt;form name=form1&gt;  
&lt;select name=select1 size=4  
onChange=document.form1.text1.value=document.form1.select1.value&gt;  
&lt;option value="北京"&gt;北京&lt;/option&gt;  
&lt;option value="上海"&gt;上海&lt;/option&gt;  
&lt;option value="武汉"&gt;武汉&lt;/option&gt;  
&lt;option value="天津"&gt;天津&lt;/option&gt;  
&lt;option value="大连"&gt;大连&lt;/option&gt;  
&lt;/select&gt;  
&lt;input tppe=text name=text1 value=hello&gt;  
&lt;/form&gt;

7.创建网页加载和卸载信息

&lt;body onload=document.form1.text1.value='页面加载完毕' onunload=alert('再见,欢迎再来')&gt;  
&lt;form name=form1&gt;  
&lt;input type=text name=text1 value="页面正在加载 ……"&gt;  
&lt;/form&gt;

第七章 使用对象

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

&lt;body bgcolor="green"&gt;  
&lt;script&gt;  
document.write("页面背景颜色是:"+document.bgColor)  
document.write("页面前景颜色是:"+document.fgColor)  
&lt;/script&gt;

2.使用网页元素对象

&lt;script&gt;  
&lt;/script&gt;  
&lt;form name=form1&gt;  
&lt;textarea name=ta1&gt;dfgfdgfdhfdhdfdfgdf&lt;/textarea&gt;  
&lt;input type=button value="选择文本" onclick=document.form1.ta1.select()&gt;  
&lt;input type=button value="显示文本" onclick=document.write(document.form1.ta1.value)&gt;  
&lt;/form&gt;

3.使用子对象

&lt;form name=form1&gt;  
&lt;input type=text name=text1 value=hello&gt;  
&lt;/form&gt;  
&lt;script&gt;  
document.form1.text1.value="gdfgfd"  
&lt;/script&gt;

&lt;form name=form1&gt;  
&lt;input type=radio name=radio1&gt;男  
&lt;input type=radio name=radio2&gt;女  
&lt;/script&gt;  
&lt;script&gt;  
document.form1.radio1.checked=true  
&lt;/script&gt;

4.使用预定义对象

&lt;script&gt;  
str1="dgdfgdfgdfhf固定法固定法功夫攻打法"  
document.write(str1+"&lt;br&gt;")  
str2=str1.substr(5)  
document.write(str2+"&lt;br&gt;")  
document.write("输出圆的面积:"+Math.PI\*Math.pow(5.0,2))  
&lt;/script&gt;

5.创建新对象

&lt;script&gt;  
today=new Date()  
document.write("今天是"+(today.getMonth()+1)+"月"+today.getDate()+"日"+"&lt;br&gt;")  
document.write("现在是:"+today.toLocaleString())  
&lt;/script&gt;

6.引用当前对象

&lt;form name=form1&gt;  
&lt;input type=text name=text1 value="dgdgdfgfd" onclick=this.select()&gt;  
&lt;/script&gt;

7.查看对象属性

&lt;script&gt;  
for(prop in window)  
{document.write("window."+prop+"="+window\[prop]+"&lt;br&gt;");}  
for(prop2 in location)  
{document.write("location."+prop2+"="+location\[prop]+"&lt;br&gt;");}  
&lt;/script&gt;

8.使用Array对象

&lt;script&gt;  
array=new Array(10)  
array\[0]="bark"  
array\[1]="apple"  
array\[2]="nebula"  
array\[3]="cookie"  
array\[4]="technology"  
document.write("数组元素个数是"+array.Length+"&lt;br&gt;")  
document.write("用 join将数组合并"+array.join(" ")+"&lt;br&gt;")  
document.write(" 数组排序"+array.sort())  
&lt;/script&gt;

9.使用 image 对象

&lt;img src=\*\*.gif alt="图片提示…." border=10&gt;  
&lt;script&gt;  
document.write("图片提示是:"+document.images\[0].alt+"&lt;br&gt;")  
document.write("图片边框大小是:"+document.images\[0].broder)  
&lt;/script&gt;

10.预加载图像

&lt;script&gt;  
freddy=new Image()  
freddy.src=freddy.gif  
&lt;/script&gt;  
&lt;body onload=document.images\[0].src=freddy.src&gt;  
,&lt;img src="blank.gif"&gt;  
&lt;/body&gt;

11.改变图像

&lt;img src=freddy.gif&gt;&lt;br&gt;  
&lt;form name=form1&gt;  
&lt;input type=button name=button1 value="改变图像" onclickd=document.images\[0].src=dudjp.gif&gt;  
&lt;/form&gt;

12.使用link和anchor对象

&lt;a name=anchor1&gt;锚点1&lt;br&gt;  
&lt;a href=http://www.microsoft.com&gt;Microsoft&lt;/a&gt;&lt;br&gt;  
&lt;a href=http://www.sohu.com&gt;sohu&lt;/a&gt;&lt;br&gt;  
&lt;a href=http://www.sina.com.cn&gt;sina&lt;/a&gt;&lt;br&gt;  
&lt;script&gt;  
document.write("本页面共有"+document.links.length+"链接"+"&lt;br&gt;")  
document.write("本页面共有"+document.anchors.length+"锚点"+"&lt;br&gt;")  
document.write("第一个链接协议是"+document.links\[0].protocol+"&lt;br&gt;")  
document.write("第一个链接路径是"+document.links\[0].pathnamel+"&lt;br&gt;")  
document.write("第一个链接href是"+document.links\[0].hrefl+"&lt;br&gt;")  
&lt;/script&gt;

13.改变链接

&lt;a href =http://www.microsoft.com&gt;link&lt;/a&gt;  
&lt;form name=form1&gt;  
&lt;input type=button name=button1 value="改变链接" onclick=document.links\[0].href='http://www.sohu.com'&gt;  
&lt;/form&gt;

14.使用history对象

&lt;form name=form1&gt;  
&lt;input type=button name=button1 value="向后返回2页" onclick=window.history.go(-2)&gt;  
&lt;/form&gt;

第八章 使用窗口

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

&lt;body onload=window.status="欢迎光临我的站点"&gt;  
&lt;a href=http://www.sohu.com&gt;sohu&lt;/a&gt;  
&lt;/body&gt;

2.改变背景色

&lt;script&gt;  
document.bgColor="orange"  
&lt;/script&gt;

3.列举背景颜色

&lt;body bgColor =green&gt;  
&lt;script&gt;  
document.write("当前背景色是:"+document.bgColor)  
&lt;/script&gt;  
&lt;/body&gt;

4.改变文本和链接颜色

&lt;script&gt;  
document.bgColor="orange"  
document.fgColor="blue"  
document.linkColor="red"  
&lt;/script&gt;  
&lt;h2&gt;看看这段文本颜色&lt;/h2&gt;  
&lt;a href=http://www.sohu.com&gt;sohu&lt;/a&gt;  
&lt;/body&gt;

5.改变文档标题

&lt;script&gt;  
name="Mouse"  
document.title="welcome to "+name+"'s House"  
document.write(document.title)  
&lt;/script&gt;

6.显示修改日期

&lt;script&gt;  
document.write("本页面最后修改时间是"+document.lastModified)  
&lt;/script&gt;

7.查看当前文档的URL

&lt;script&gt;  
document.write("本页面的[URL:"+document.URL](%22+document.URL))  
&lt;/script&gt;

8.查看引用页

&lt;script&gt;  
document.write("本页面的引用页是"+document.referrer)  
&lt;/script&gt;

9.打开新的浏览器窗口

&lt;script&gt;  
window.open("\*.htm","title","width=200,height=400,resizable=yes")  
&lt;/script&gt;

10.关闭远程窗口

close.html:  
&lt;script&gt;  
document.write("正文")  
&lt;/script&gt;  
&lt;form name=form1&gt;  
&lt;input type=button name=button1value="关闭" onclick=window.close()&gt;  
&lt;/form&gt;

open.html  
&lt;script&gt;  
window.open("close.html","romote","width=200,height=400,resizable=yes")  
&lt;/script&gt;

11.打印窗口

&lt;script&gt;  
document.write("正文")  
&lt;/script&gt;  
&lt;form name=form1&gt;  
&lt;input type=button value=打印 onclick=window.print()&gt;  
&lt;/form&gt;

12.移动窗口

&lt;form name=form1&gt;  
水平方向&lt;input type=text name=x value=20&gt;  
垂直方向&lt;input type=text name=y value=50&gt;  
&lt;input type=button value="移动窗口到…"onclick=window.moveTo(document.form1.x.value,document.form1.y.value)&gt;  
&lt;/form&gt;

&lt;form name=form1&gt;  
水平方向&lt;input type=text name=x value=20&gt;  
垂直方向&lt;input type=text name=y value=50&gt;  
&lt;input type=button value="移动窗口"onclick=window.moveBy(document.form1.x.value,document.form1.y.value)&gt;  
&lt;/form&gt;

13.改变窗口大小

&lt;form name=form1&gt;  
水平方向&lt;input type=text name=x value=200&gt;  
垂直方向&lt;input type=text name=y value=500&gt;  
&lt;input type=button value="改变窗口大小到….."onclick=window.resizeTo(document.form1.x.value,document.form1.y.value)&gt;  
&lt;/form&gt;

&lt;form name=form1&gt;  
水平方向&lt;input type=text name=x value=200&gt;  
垂直方向&lt;input type=text name=y value=500&gt;  
&lt;input type=button value="改变窗口大小"onclick=window.resizeBy(document.form1.x.value,document.form1.y.value)&gt;  
&lt;/form&gt;

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

&lt;script&gt;  
window.alert("welcome")  
&lt;/script&gt;

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

&lt;script&gt;  
name=window.prompt("输入姓名","姓名")  
document.write(" 欢迎您:"+name+"来到这里")  
&lt;/script&gt;

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

&lt;script&gt;  
like=window.confirm("你觉得好吗?")  
if(like==true)  
document.write("谢谢你的夸奖")  
else  
document.write("希望得到你的夸奖")  
&lt;/script&gt;

第九章 使用字符串

1.使用字符串对象

&lt;script&gt;  
mystring="gdgdfgfddddaaaaaaaaaaaabbbbbbbbbbbbbbbbbvbhg.&lt;br&gt;"  
document.write(mystring)  
document.write(mystring.bold())  
document.write(mystring.toUpperCase())  
&lt;/script&gt;

2.使用子字符串

&lt;script&gt;  
str1="fdsf 1111 gfdgfd dfdsf cccc dddd.&lt;br&gt;"  
document.write(str1)  
document.write(str1.substring(0,13)+"&lt;br&gt;")  
document.write(str1.substr (20,11)+"&lt;br&gt;")  
&lt;/script&gt;

3.连接字符串

&lt;script&gt;  
str1="may you find"  
str2="peace,happiness and prosperity.&lt;br&gt;"  
document.write(str1+"&lt;br&gt;")  
document.write(str2)  
document.write(str1.concat(str2))  
document.write(str1+=str2)  
&lt;/script&gt;

4.格式化字符串变量

&lt;script&gt;  
str1="peace,happiness and prosperity.&lt;br&gt;"  
document.write(str1)  
document.write(str1.big())  
document.write(str1.small())  
document.write(str1.bold())  
document.write(str1.italics())  
document.write(str1.strike())  
document.write(str1.fontsize(6))  
document.write(str1.fontcolor(green))  
&lt;/script&gt;

5.创建锚和链接

&lt;script&gt;  
str1="this is the bigginning of the page.&lt;br&gt;"  
str2="….&lt;br&gt;"  
str3="this is the end of the page .&lt;br&gt;"  
str4="link to the start&lt;br&gt;"  
str5="link to the end&lt;br&gt;"  
document.write(str1.anchor("start"))  
for(i=0;i&lt;10;i++)  
document.write(str2);  
document.write(str3.anchor("end"))  
document.write(str4.link("#start"))  
document.write(str5.link("#end"))  
&lt;/script&gt;

6.确定字符串长度

&lt;script&gt;  
str1="this is the bigginning of the page."  
document.write(str1+"&lt;br&gt;")  
document.write( "字符串的长度是:"+str1.length)  
document.write("字符串全部大写是;"+str1.toUpperCase())  
document.write("字符串全部小写是;"+str1.toLowerCase())  
&lt;/script&gt;

7.在字符串内搜索

&lt;script&gt;  
str1="this is the end of the line.&lt;br&gt;"  
document.write(str1)  
document.write("字符end在字符串的位置是"+str1.search("end"))  
document.write("字符dog在字符串的位置是"+str1.search("dog"))  
&lt;/script&gt;

8.定位字符串中的字符

&lt;script&gt;  
str1="spring is a time for flowers and trees and baby bunnles&lt;br&gt;"  
document.write(str1)  
document.write("the index for the second word ‘and' is"+str1.indexOf("and",30))  
documednt.write("the last index of the word ‘and' is "+str1.lastIndexOf("and"))  
&lt;/script&gt;

9.替换字符串中的文本

&lt;script&gt;  
str1="spring is a time for flowers and trees and baby bunnles&lt;br&gt;"  
document.write(str1)  
document .write(str1.replace("and",","))  
&lt;/script&gt;

10.字符串分离

&lt;script&gt;  
str1="spring is a time for flowers and trees and baby bunnles&lt;br&gt;"  
document.write(str1)  
str1array=str1.split(" ")  
document.write(str1array\[0]+"&lt;br&gt;")  
document.write(str1array\[1]+"&lt;br&gt;")  
document.write(str1array\[2]+"&lt;br&gt;")  
document.write(str1array\[3]+"&lt;br&gt;")  
&lt;/script&gt;

第十章 使用日期和时间

1.使用Date对象

&lt;script&gt;  
cdate=new Date("august 2,1989 12:30:00")  
document.write(cdate)  
&lt;/script&gt;

2.显示当地时间和日期

&lt;script&gt;  
cdate=new Date()  
document.write("当前时间是:"+cdate.toGMTString()+"&lt;br&gt;")  
document.write("日期和时间是:"+cdate.toLocaleString())  
&lt;/script&gt;

3.获得时间和日期值

&lt;script&gt;  
cdate=new Date()  
document.write("显示当前的星期"+cdate.getDay()+"&lt;br&gt;")  
document.write("显示当前的月份"+cdate.getMonth()+"&lt;br&gt;")  
document.write("显示当前的日期"+cdate.getDay()+"&lt;br&gt;")  
document.write("显示当前的年份"+cdate.getYear()+"&lt;br&gt;")  
document.write("显示当前的小时"+cdate.getHours()+"&lt;br&gt;")  
document.write("显示当前的分钟"+cdate.getMinutes()+"&lt;br&gt;")  
document.write("显示当前的秒"+cdate.getSeconds()+"&lt;br&gt;")  
&lt;/script&gt;

4.设置时间和日期值

&lt;script language=javascript&gt;  
cdate=new Date("December 25,1984")  
document.write("显示日期"+cdate+"&lt;br&gt;")  
document.write("设置月份"+cdate.setMonth(10)+"&lt;br&gt;")  
document.write("设置日期"+cdate.setDate(23)+"&lt;br&gt;")  
document.write("设置年份"+cdate.setYear(2000)+"&lt;br&gt;")  
document.write("设置小时"+cdate.setHours(13)+"&lt;br&gt;");  
document.write("设置分钟"+cdate.setMinutes(47)+"&lt;br&gt;");  
document.write("设置秒"+cdate.setSeconds(23)+"&lt;br&gt;");  
document.write("显示设置后的日期和时间"+cdate);  
&lt;/script&gt;

第十一章 使用Math对象

1\. 使用Math对象

&lt;script language=javascript&gt;  
&lt;/script&gt;  
&lt;form name=form1&gt;  
圆的半径:&lt;input type=text name=rad&gt;&lt;br&gt;  
圆的面积:&lt;input type=text name=area&gt;&lt;br&gt;  
&lt;input type=button name=button1 value=计算圆的面积 onclick=document.form1.area.value=document.form1.rad.value\*document.  
form1.rad.value\*Math.PI&gt;  
&lt;/form&gt;

2.生成随机数

&lt;script&gt;  
array1=new Array(  
"这是第1句",  
"这是第2句",  
"这是第3句",  
"这是第4句",  
"这是第5句",  
"这是第6句")  
RandomNo=Math.floor(array1.length\*Math.random())  
document.write("随机输出某一句"+"&lt;br&gt;"+array1\[RandomNo])  
&lt;/script&gt;

3.使用平方根

&lt;form name=form1&gt;  
value:&lt;input type=text name=va1&gt;&lt;br&gt;  
平方根&lt;input type=text name=sqrt&gt;&lt;br&gt;  
&lt;input type=button name=button1 value=计算平方根  
onclick="document.form1.sqrt.value=Math.sqrt(document.form1.va1.value)"&gt;  
&lt;/form&gt;

4.数字的舍入

&lt;form name=form1&gt;  
输入&lt;input type=text name=val&gt;&lt;br&gt;  
舍入的结果&lt;input type=text name=round&gt;&lt;br&gt;  
&lt;input type=button name=button1 value=计算结果 onclick=document.form1.round.value=Math.round(document.form1.val.value)&gt;  
&lt;/form&gt;

5.乘方运算

&lt;form name=form1&gt;  
底数&lt;input type=text name=val&gt;&lt;br&gt;  
指数&lt;input type=text name=power&gt;&lt;br&gt;  
幂&lt;input type=text name=result&gt;&lt;br&gt;  
&lt;input type=button name=button1 value=计算结果 onclick="document.form1.result.value=Math.pow (document.form1.val.value,document.form1.power.value)"&gt;  
&lt;/form&gt;

6.发现最小值和最大值

&lt;form name=form1&gt;  
数字1&lt;input type=text name=val1&gt;&lt;br&gt;  
数字2&lt;input type=text name=val2&gt;&lt;br&gt;  
最小值&lt;input type=text name=min&gt;&lt;br&gt;  
最大值&lt;input type=text name=max&gt;&lt;br&gt;  
数字1&lt;input type=button value=计算 onclick="document.form1.min.value=Math.min (document.form1.val1.value,document.form1.val2.value);document.form1.  
max.value= Math.max(document.form1.val1.value,document.form1.val2.value)"&gt;  
&lt;/form&gt;

第十二章 使用表单

1.使用文本框

&lt;form name=form1&gt;  
&lt;input type=text value="information ,please"name=text1&gt;  
&lt;/form&gt;  
&lt;script&gt;  
document.write("表单text1类型是: "+document.form1.text1.type+"&lt;br&gt;")  
document.write("表单text1名称是: "+document.form1.text1.name+"&lt;br&gt;")  
document.write("表单text1值是: "+document.form1.text1.value+"&lt;br&gt;")  
document.write("表单text1大小是: "+document.form1.text1.size+"&lt;br&gt;")  
&lt;/script&gt;

&lt;form name=form1&gt;  
&lt;input type=text name=text1 value=click here  
onfocus=document.form1.text1.select()&gt;  
&lt;/form&gt;

2.使用密码框

&lt;form name=form1&gt;  
&lt;input type=password name=pw1 value=daylight&gt;  
&lt;/form&gt;  
&lt;script&gt;  
document.write("表单pw1的类型:"+document.form1.pw1.type+"&lt;br&gt;")  
document.write("表单pw1的名称:"+document.form1.pw1.name+"&lt;br&gt;")  
document.write("表单pw1的值:"+document.form1.pw1.value+"&lt;br&gt;")  
document.write("表单pw1的大小:"+document.form1.pw1.size+"&lt;br&gt;")  
&lt;/script&gt;

3.使用隐藏字段

&lt;form name=form1&gt;  
&lt;input type=hidden name=hid1 value=piece of eight&gt;  
&lt;/form&gt;  
&lt;script&gt;  
document.write("表单hid1的类型:"+document.form1.hid1.type+"&lt;br&gt;")  
document.write("表单hid1的名称:"+document.form1.hid1.name+"&lt;br&gt;")  
document.write("表单hid1的值:"+document.form1.hid1.value+"&lt;br&gt;")  
&lt;/script&gt;

4.使用文本区域框

&lt;form name=form1&gt;  
&lt;textarea name=ta1&gt;how many grains of sand are there in the sahara desert?&lt;/textarea&gt;  
&lt;/form&gt;  
&lt;script&gt;  
document.write("表单ta1的类型:"+document.form1.ta1.type+"&lt;br&gt;")  
document.write("表单ta1的名称:"+document.form1.ta1.name+"&lt;br&gt;")  
document.write("表单ta1的值:"+document.form1.ta1.value+"&lt;br&gt;")  
document.write("表单ta1的横向宽度:"+document.form1.ta1.cols+"&lt;br&gt;")  
document.write("表单ta1的纵向宽度:"+document.form1.rows.value+"&lt;br&gt;")  
&lt;/script&gt;

6.使用重置按钮

&lt;form name=form1&gt;  
&lt;input type=reset name=reset1 value="rest form"&gt;  
&lt;/form&gt;  
&lt;script&gt;  
document.write("表单reset1的类型:"+document.form1.reset1.type+"&lt;br&gt;")  
document.write("表单reset1的名称:"+document.form1.reset1.name+"&lt;br&gt;")  
document.write("表单reset1的值:"+document.form1.reset1.value+"&lt;br&gt;")  
&lt;/script&gt;

7.使用提交按钮

&lt;form name=form1&gt;  
&lt;input type=submit name=submit1 value="submit form"&gt;  
&lt;/form&gt;  
&lt;script&gt;  
document.write("表单submit1的类型:"+document.form1.submit1.type+"&lt;br&gt;")  
document.write("表单submit1的名称:"+document.form1.submit1.name+"&lt;br&gt;")  
document.write("表单submit1的值:"+document.form1.submit1.value+"&lt;br&gt;")  
&lt;/script&gt;

8.使用复选按钮

&lt;form name=form1&gt;  
&lt;input type=checkbox name=cb1 &gt;computer savvy?  
&lt;/form&gt;  
&lt;script&gt;  
document.write("表单cb1的类型:"+document.form1.cb1.type+"&lt;br&gt;")  
document.write("表单cb1是否被选择?:"+document.form1.cb1.checked+"&lt;br&gt;")  
document.write("表单cb1的名称:"+document.form1.cb1.name+"&lt;br&gt;")  
&lt;/script&gt;

9.使用单选按钮

&lt;form name=form1&gt;  
&lt;input type=radio name=radio1&gt;male  
&lt;input type=radio name=radio1&gt;female  
&lt;/form&gt;  
&lt;script&gt;  
document.write("第一个按钮被选择"+document.form1.radio1\[0].checked+"&lt;br&gt;")  
document.write("第二个按钮被选择"+document.form1.radio1\[1].checked+"&lt;br&gt;")  
document.write("按钮的名称"+ document.form1.radio1\[0].name+"&lt;br&gt;")  
document.write("按钮的个数"+document.form1.radio1.length)  
&lt;/script&gt;

10.使用选择列表

&lt;form name=form1&gt;  
&lt;select name=select1 size=4&gt;  
&lt;option name=option1 value=lon&gt;london,England&lt;/option&gt;  
&lt;option name=option2 value=dub&gt;Dublin,Ireland&lt;/option&gt;  
&lt;/select&gt;  
&lt;/form&gt;  
&lt;script&gt;  
document.write("这个选择列表的名称"+document.form1.select1.name+"&lt;br&gt;")  
document.write("这个选择列表的长度"+document.form1.select1.length+"&lt;br&gt;")  
document.write("这个选择列表当前被选择的索引号"+document.form1.select1.selectedIndex+"&lt;br&gt;")  
document.write("这个选择列表的尺寸"+document.form1.select1.size+"&lt;br&gt;")  
&lt;/script&gt;

11.验证表单的有效性

&lt;script&gt;  
function validate(){  
if(document.form1.text1.value!='1'||'2'||'3'||'4'){  
alert("请输入1~4的整数")  
}  
}  
&lt;/script&gt;  
&lt;form name=form1&gt;  
请输入1~4的整数:  
&lt;input type=text name=text1 size=4 onchange=validate()&gt;  
&lt;/form&gt;

12.控制表单焦点

&lt;form name=form1&gt;  
&lt;input type=text name=text1 value=where is you focus?&gt;&lt;br&gt;  
&lt;input type=text name=text2 value=is there?&gt;&lt;br&gt;  
&lt;input type=text name=text3 value=or maybe here?&gt;&lt;br&gt;  
&lt;input type=button name=button1 value="text box #1" onclick=document.form1.text1.focus()&gt;&lt;br&gt;  
&lt;input type=button name=button2 value="text box #2" onclick=document.form1.text2.focus()&gt;&lt;br&gt;  
&lt;input type=button name=button3 value="text box #3" onclick=document.form1.text3.focus()&gt;&lt;br&gt;  
&lt;/form&gt;

第十三章 使用分栏

第十四章 使用navigator

1.使用navigator对象

&lt;script&gt;  
document.write("navigator对象的属性"+"&lt;br&gt;")  
document.write("appcodename:"+navigator.appCodeName+"&lt;br&gt;")  
document.write("appname::"+navigator.appName+"&lt;br&gt;")  
document.write("appversion:"+navigator.appVersion+"&lt;br&gt;")  
document.write("platform:"+navigator.platform+"&lt;br&gt;")  
document.write("userAgent:"+navigator.userAgent+"&lt;br&gt;")  
&lt;/script&gt;  
&lt;script&gt;  
document.write("navigator对象的方法"+"&lt;br&gt;")  
document.write("javaEnabled():"+navigator.javaEnabled())  
&lt;/script&gt;

2.检查用户的浏览器

&lt;script&gt;  
if(navigator.appName.indexOf("Microsoft")!=-1){  
document.write("用户浏览器是微软的IE浏览器"+"&lt;br&gt;")}  
else if(navigator.appName.indexOf("Netscape")!=-1){  
document.write("用户浏览器是netscape的netscape浏览器"+"&lt;br&gt;")}  
if(navigator.appVersion.indexOf("4.0")!=-1){  
document.write("you are using a version 4.0compatible browser")  
}  
else{  
document.write("this browser is not 4.0 compliant")}  
&lt;/script&gt;

3.检测用户的操作系统

&lt;script&gt;  
if (navigator.platform.indexOf("win32")!=-1){  
document.write("you are using a computer running windows 95 or highter")}  
else{  
document.write("this computer is not running windows 95 or higher")}  
&lt;/script&gt;

4.使用location对象

&lt;script&gt;  
document.write("location对象的属性"+"&lt;br&gt;")  
document.write("hash"+location.hash+"&lt;br&gt;")  
document.write("hostname"+location.hostname+"&lt;br&gt;")  
document.write("host"+location.host+"&lt;br&gt;")  
document.write("href"+location.href+"&lt;br&gt;")  
document.write("port"+location.port+"&lt;br&gt;")  
document.write("search"+location.search+"&lt;br&gt;")  
&lt;/script&gt;

重新加载网页  
&lt;form name=form1&gt;  
&lt;input type=button name=button1 value=重新加载本页 onclick=location.reload&gt;  
&lt;/form&gt;

5.使用cookie

&lt;script&gt;  
finction makecookie(){  
if(!document.cookie){  
name=prompt("请输入你的姓名");  
document.cookie="name="+name+";";}  
}  
&lt;/script&gt;

&lt;body onload=makecookie()&gt;  
&lt;script&gt;  
function makecookie(){  
if(!document.cookie){  
name=prompt("请输入你的姓名")  
document.cookie="name="+name+";";  
namestart=document.cookie.indexOf("=");  
nameend=document.cookieindexOf(";");  
document.writeln("your name is:"+document.cookie.substring(namestart+1,nameend)+",br&gt;")  
}  
}  
&lt;/script&gt;