清华大学出版的《事半功倍系列 javascript》,本人照着书敲出来的,有些翻译了一下.前几年看了一下,最近无事,重新翻了翻,很有帮助.本书应该有光盘的,但学校的书,光盘不知在哪.希望对你学 javascript有帮助
第一章javascript简介
第一章javascript简介
1 1.在地址栏输入javascript语句
2
3 Javascript:Document.write("显示文字")
4
5 2.将javascript嵌入 HTML文档
6
7 <script language=javascript>
8 document.bgColor="blue"
9 </script>
10
2
3 Javascript:Document.write("显示文字")
4
5 2.将javascript嵌入 HTML文档
6
7 <script language=javascript>
8 document.bgColor="blue"
9 </script>
10
第二章 使用变量和数组
1.声明变量
1 <script language=javascripe>
2 Var answer1,answer2,answer3,answer4;
3 answer1=9;
4 answer2=2.5
5 answer3="Milkey May"
6 answer4=true
7 </script>
2.使用整数
2 Var answer1,answer2,answer3,answer4;
3 answer1=9;
4 answer2=2.5
5 answer3="Milkey May"
6 answer4=true
7 </script>
1 <script language=javascript>
2 var decimalNum,hexadecimalNum,octalNum
3 decimalNum=24
4 hexadecimalNum=0x24
5 octalNum=024
6 document.write("显示十进制数:"+ decimalNum+"<br>")
7 document.write("显示十六进制数:"+ hexadecimalNum +"<br>")
8 document.write("显示八进制数:"+ octalNum +"<br>")
9 </script>
3.使用浮点数
2 var decimalNum,hexadecimalNum,octalNum
3 decimalNum=24
4 hexadecimalNum=0x24
5 octalNum=024
6 document.write("显示十进制数:"+ decimalNum+"<br>")
7 document.write("显示十六进制数:"+ hexadecimalNum +"<br>")
8 document.write("显示八进制数:"+ octalNum +"<br>")
9 </script>
1 <script language=javascript>
2 var num1,num2,num3,num4
3 num1=1234567890000.0
4 num2=5.14e23
5 num3=0.0000123456
6 num4=6.0254e3-4
7 document.write("浮点数1:"+num1+"<br>")
8 document.write("浮点数2:"+num2+"<br>")
9 document.write("浮点数3:"+num3+"<br>")
10 document.write("浮点数4:"+num4+"<br>")
11 </script>
12
4.使用布尔值
2 var num1,num2,num3,num4
3 num1=1234567890000.0
4 num2=5.14e23
5 num3=0.0000123456
6 num4=6.0254e3-4
7 document.write("浮点数1:"+num1+"<br>")
8 document.write("浮点数2:"+num2+"<br>")
9 document.write("浮点数3:"+num3+"<br>")
10 document.write("浮点数4:"+num4+"<br>")
11 </script>
12
1 <script language=javascript>
2 var answer1,answer2
3 answer1=true
4 answer2=false
5 document.write("显示布尔1:"+answer1+"<br>")
6 document.write("显示布尔2:"+answer2+"<br>")
7 </script>
5.使用字符串
2 var answer1,answer2
3 answer1=true
4 answer2=false
5 document.write("显示布尔1:"+answer1+"<br>")
6 document.write("显示布尔2:"+answer2+"<br>")
7 </script>
1<script language=javascript>
2var str1,str2
3str1="fdsgdg dsfdsf china"
4str2="武汉市广播电视大学"
5document.write("显示字符串1:"+str1+"<br>")
6document.write("显示字符串2:"+str2+"<br>")
7</script>
6.确定变量类型
2var str1,str2
3str1="fdsgdg dsfdsf china"
4str2="武汉市广播电视大学"
5document.write("显示字符串1:"+str1+"<br>")
6document.write("显示字符串2:"+str2+"<br>")
7</script>
1 <script>
2 var answer1,answer2,answer3,answer4
3 answer1=9
4 answer2=2.5
5 answer3="milky may"
6 answer4=true
7 document.write("变量1的类型是:"+typeof answer1 +"<br>")
8 document.write("变量2的类型是:"+typeof answer2 +"<br>")
9 document.write("变量3的类型是:"+typeof answer3 +"<br>")
10 document.write("变量4的类型是:"+typeof answer4 +"<br>")
11 </script>
7.将字符串转换成数字
2 var answer1,answer2,answer3,answer4
3 answer1=9
4 answer2=2.5
5 answer3="milky may"
6 answer4=true
7 document.write("变量1的类型是:"+typeof answer1 +"<br>")
8 document.write("变量2的类型是:"+typeof answer2 +"<br>")
9 document.write("变量3的类型是:"+typeof answer3 +"<br>")
10 document.write("变量4的类型是:"+typeof answer4 +"<br>")
11 </script>
1 1 <script>
2 2 var str1="31 days in january"
3 3 var int1=parseInt(str1)
4 4 document.write("str1的数据类型是 :"+typeof str1+"<br>")
5 5 document.write("int1的数据类型是 :"+typeof int1+"<br>")
6 6 </script>
8.将数字转换成字符串
2 2 var str1="31 days in january"
3 3 var int1=parseInt(str1)
4 4 document.write("str1的数据类型是 :"+typeof str1+"<br>")
5 5 document.write("int1的数据类型是 :"+typeof int1+"<br>")
6 6 </script>
1<script>
2var int1=256
3var str1=""+int1
4document.write("str1的数据类型是 :"+typeof str1+"<br>")
5document.write("int1的数据类型是 :"+typeof int1+"<br>")
6</script>
9.声明数组
2var int1=256
3var str1=""+int1
4document.write("str1的数据类型是 :"+typeof str1+"<br>")
5document.write("int1的数据类型是 :"+typeof int1+"<br>")
6</script>
1 <script>
2 array=new Array(5)
3 array[0]=1
4 array[1]=3
5 array[2]=5
6 array[3]=7
7 array[4]=11
8 document.write("数组是:"+array[0]+" "+array[1]+" "+array[2]+" "+array[3]+" "+array[4])
9 </script>
10
11
10.确定数组元素的个数
2 array=new Array(5)
3 array[0]=1
4 array[1]=3
5 array[2]=5
6 array[3]=7
7 array[4]=11
8 document.write("数组是:"+array[0]+" "+array[1]+" "+array[2]+" "+array[3]+" "+array[4])
9 </script>
10
11
1 <script>
2 array=new Array(5)
3 array[0]=1
4 array[1]=3
5 array[2]=5
6 array[3]=7
7 array[4]=11
8 document.write("数组是:"+array[0]+" "+array[1]+" "+array[2]+" "+array[3]+" "+array[4]+"<br>")
9 document.write("数组的元素个数是"+array.length)
10 </script>
11
12
11.将数组转换为字符串
2 array=new Array(5)
3 array[0]=1
4 array[1]=3
5 array[2]=5
6 array[3]=7
7 array[4]=11
8 document.write("数组是:"+array[0]+" "+array[1]+" "+array[2]+" "+array[3]+" "+array[4]+"<br>")
9 document.write("数组的元素个数是"+array.length)
10 </script>
11
12
1 <script>
2 array=new Array()
3 array[0]="dark"
4 array[1]="apple"
5 array[2]="nebula"
6 array[3]="water"
7 str1=array.join()
8 str2=array.join(" ")
9 document.write(str1+"<br>")
10 document.write(str2)
11 </script>
12.对数组排序
2 array=new Array()
3 array[0]="dark"
4 array[1]="apple"
5 array[2]="nebula"
6 array[3]="water"
7 str1=array.join()
8 str2=array.join(" ")
9 document.write(str1+"<br>")
10 document.write(str2)
11 </script>
1 <script>
2 array=new Array()
3 array[0]="dark"
4 array[1]="apple"
5 array[2]="nebula"
6 array[3]="water"
7 str1=array.sort()
8 document.write(str1+"<br>")
9 </script>
2 array=new Array()
3 array[0]="dark"
4 array[1]="apple"
5 array[2]="nebula"
6 array[3]="water"
7 str1=array.sort()
8 document.write(str1+"<br>")
9 </script>
第三章 创建表达式
1.使用算术运算符
1 <script>
2 var1=12
3 var2=10
4 varadd=var1+var2
5 varsub=var1-var2
6 varmult=var1*var2
7 vardiv=var1/var2
8 varmod=var1%var2
9 document.write("数据1是:"+var1+"<br>")
10 document.write("数据2是:"+var2+"<br>")
11 document.write("数据相加是:"+varadd+"<br>")
12 document.write("数据相减是:"+varsub+"<br>")
13 document.write("数据相乘是:"+varmult+"<br>")
14 document.write("数据相除是:"+vardiv+"<br>")
15 document.write("数据相除取余数是:"+varmod+"<br>")
16 </script>
17
18
2.递增变量和递减变量
2 var1=12
3 var2=10
4 varadd=var1+var2
5 varsub=var1-var2
6 varmult=var1*var2
7 vardiv=var1/var2
8 varmod=var1%var2
9 document.write("数据1是:"+var1+"<br>")
10 document.write("数据2是:"+var2+"<br>")
11 document.write("数据相加是:"+varadd+"<br>")
12 document.write("数据相减是:"+varsub+"<br>")
13 document.write("数据相乘是:"+varmult+"<br>")
14 document.write("数据相除是:"+vardiv+"<br>")
15 document.write("数据相除取余数是:"+varmod+"<br>")
16 </script>
17
18
1 <script>
2 days=1
3 document.write("输出变量"+days+"<br>")
4 days++
5 document.write("递增后变量变为:"+days)
6 </script>
7
8 3.创建比较表达式
9
10 <script>
11 daysofmonth=28
12 if(daysofmonth==28)
13 month="february"
14 document.write("days of month:"+daysofmonth+"<br>")
15 document.write("month:"+month)
16 </script>
17
18
4.创建逻辑表达式
2 days=1
3 document.write("输出变量"+days+"<br>")
4 days++
5 document.write("递增后变量变为:"+days)
6 </script>
7
8 3.创建比较表达式
9
10 <script>
11 daysofmonth=28
12 if(daysofmonth==28)
13 month="february"
14 document.write("days of month:"+daysofmonth+"<br>")
15 document.write("month:"+month)
16 </script>
17
18
1 <script>
2 dayofmonth=28
3 if(dayofmonth==28 || dayofmonth==29)
4 month="february"
5 document.write("days of month:"+dayofmonth+"<br>")
6 document.write("month:"+month)
7 </script>
5.使用条件运算符
2 dayofmonth=28
3 if(dayofmonth==28 || dayofmonth==29)
4 month="february"
5 document.write("days of month:"+dayofmonth+"<br>")
6 document.write("month:"+month)
7 </script>
1 <script language="javascript">
2 stomach="hungry";
3 time="5:00";
4 (stomach=="hungry"&&time=="5:00") ? eat = "dinner":eat="a snack";
5 document.write("输出结果"+eat);
6 </script>
7
8
6.识别数字
2 stomach="hungry";
3 time="5:00";
4 (stomach=="hungry"&&time=="5:00") ? eat = "dinner":eat="a snack";
5 document.write("输出结果"+eat);
6 </script>
7
8
1 <script>
2 var1=24;
3 (isNaN(var1))?document.write("变量var1"+var1+"不是数字"):Document.write("变量var1"+var1+"是数字")
4 </script>
5
6
2 var1=24;
3 (isNaN(var1))?document.write("变量var1"+var1+"不是数字"):Document.write("变量var1"+var1+"是数字")
4 </script>
5
6
第四章 控制程序流程
1.使用IF –Else语句
1 <script>
2 month="december"
3 date=25
4 if(month=="december" && date==25)
5 document.write("今天是圣诞节,商店关门")
6 else
7 document.write("欢迎,您来商店购物")
8 </script>
9
10
2.使用for 循环
2 month="december"
3 date=25
4 if(month=="december" && date==25)
5 document.write("今天是圣诞节,商店关门")
6 else
7 document.write("欢迎,您来商店购物")
8 </script>
9
10
1 <script>
2 for (count=1;count<=10;count++)
3 document.write("输出第"+count+"句"+"<br>")
4 </script>
3.使用while循环
2 for (count=1;count<=10;count++)
3 document.write("输出第"+count+"句"+"<br>")
4 </script>
1 <script>
2 count=1
3 while(count<=15){
4 document.write("输出第"+count+"句" +"<br>")
5 count++}
6 </script>
4.中断循环
2 count=1
3 while(count<=15){
4 document.write("输出第"+count+"句" +"<br>")
5 count++}
6 </script>
1 <script>
2 count=1
3 while(count<=15){
4 count++
5 if(count==8)
6 break;
7 document.write("输出第"+count+"句"+"<br>")}
8 </script>
5.继续循环
2 count=1
3 while(count<=15){
4 count++
5 if(count==8)
6 break;
7 document.write("输出第"+count+"句"+"<br>")}
8 </script>
1 <script>
2 count=1
3 while(count<=15){
4 count++
5 if(count==8)
6 continue;
7 document.write("输出第"+count+"句"+"<br>")}
8 </script>
2 count=1
3 while(count<=15){
4 count++
5 if(count==8)
6 continue;
7 document.write("输出第"+count+"句"+"<br>")}
8 </script>
6.使用javascript定时器
1 <script>
2 function rabbit()
3 {document.write("输出语句")
4 }
5 </script>
6 <body onload=window.setTimeout(rabbit(),5000)>
7.设置定期间隔
2 function rabbit()
3 {document.write("输出语句")
4 }
5 </script>
6 <body onload=window.setTimeout(rabbit(),5000)>
1 <script>
2 window.setInterval("document.form1.text2.value=document.form1.text1.value",3000)
3 </script>
4 <form name=form1>
5 <input type=text name=text1><br>
6 <input type=text name=text2><br>
7 </form>
8
9
8.清除超时和间隔
2 window.setInterval("document.form1.text2.value=document.form1.text1.value",3000)
3 </script>
4 <form name=form1>
5 <input type=text name=text1><br>
6 <input type=text name=text2><br>
7 </form>
8
9
1 <script>
2 stop=window.setInterval("document.form1.text2.value=document.form1.text1.value",300)
3 </script>
4 <form name=form1>
5 <input type=text name=text1><br>
6 <input type=text name=text2><br>
7 <input type=button name=button1 value=" 清除超时和间隔" onclick=clearInterval(stop)>
8 </form>
9
10
2 stop=window.setInterval("document.form1.text2.value=document.form1.text1.value",300)
3 </script>
4 <form name=form1>
5 <input type=text name=text1><br>
6 <input type=text name=text2><br>
7 <input type=button name=button1 value=" 清除超时和间隔" onclick=clearInterval(stop)>
8 </form>
9
10
第五章 使用函数
1.声明函数
1 <script>
2 function quote()
3 { document.write("输出语句")
4 }
5 </script>
2.调用函数
2 function quote()
3 { document.write("输出语句")
4 }
5 </script>
1 <script>
2 function quote()
3 { document.write("输出语句")
4 }
5 quote()
6 </script>
2 function quote()
3 { document.write("输出语句")
4 }
5 quote()
6 </script>
3.了解全局变量和局部变量
任何不用 var关键字声明的变量都是全局变量,任何在函数外声明的变量都是全局变量
4.将参数传送给函数
1 <script>
2 function f(item)
3 {document.write("输出参数"+item+"<br>")
4 }
5 f("fgdfgd")
6 f("参数二")
7 </script>
5.从函数返回值
2 function f(item)
3 {document.write("输出参数"+item+"<br>")
4 }
5 f("fgdfgd")
6 f("参数二")
7 </script>
1 <script>
2 function average(var1,var2,var3)
3 {ave=(var1+var2+var3)/3;
4 document.write("输出结果");
5 return ave;
6 }
7 document.write(average(34,56,78))
8 </script>
6.通过HTML链接调用函数
2 function average(var1,var2,var3)
3 {ave=(var1+var2+var3)/3;
4 document.write("输出结果");
5 return ave;
6 }
7 document.write(average(34,56,78))
8 </script>
1 <script>
2 function quote(){
3 document.write(" 输出字符串")
4 }
5 </script>
6 <a href=javascript:quote()>通过HTML链接调用函数</a>
7 <a href=javascript:Document.write("输出字符")> 通过HTML链接调用函数,直接写javascript语句</a>
2 function quote(){
3 document.write(" 输出字符串")
4 }
5 </script>
6 <a href=javascript:quote()>通过HTML链接调用函数</a>
7 <a href=javascript:Document.write("输出字符")> 通过HTML链接调用函数,直接写javascript语句</a>
第六章 处理事件
1.检查鼠标单击
1 <form name=form1>
2 <input type=button name=button1 value=hello onclick=document.form1.button1.value='there'>
3 </form>
2.检测双击
2 <input type=button name=button1 value=hello onclick=document.form1.button1.value='there'>
3 </form>
1 <form name=form1>
2 <input type=button name=button1 value=hello onclick=document.form1.button1.value='你单击了按钮' ondblclick=document.form1.button1.value='你双击了该按钮'>
3 </form>
3.创建悬停按钮
2 <input type=button name=button1 value=hello onclick=document.form1.button1.value='你单击了按钮' ondblclick=document.form1.button1.value='你双击了该按钮'>
3 </form>
1 <img src=go.gif onmouseover=document.images[0].src='go2.gif' onmouseout= document.images[0].src='go.gif'>
4.检测按键
1 <form name=form1>
2 <input type=text name=text1 value=hello onkeypress="if(window.event.keyCode=='100') document.form1.text1.value='你按了d键'">
3 </form>
5.设置焦点
2 <input type=text name=text1 value=hello onkeypress="if(window.event.keyCode=='100') document.form1.text1.value='你按了d键'">
3 </form>
1 <form name=form1>
2 <input type=text name=text1 value=hello
3 onfous=document.form1.text1.value='该文本框获得焦点'
4 onblur=document.form1.text1.value='该文本框失去焦点'>
5 </form>
6.检测下拉菜单选择
2 <input type=text name=text1 value=hello
3 onfous=document.form1.text1.value='该文本框获得焦点'
4 onblur=document.form1.text1.value='该文本框失去焦点'>
5 </form>
1 <form name=form1>
2 <select name=select1 size=4
3 onChange=document.form1.text1.value=document.form1.select1.value>
4 <option value="北京">北京</option>
5 <option value="上海">上海</option>
6 <option value="武汉">武汉</option>
7 <option value="天津">天津</option>
8 <option value="大连">大连</option>
9 </select>
10 <input tppe=text name=text1 value=hello>
11 </form>
12
13
7.创建网页加载和卸载信息
2 <select name=select1 size=4
3 onChange=document.form1.text1.value=document.form1.select1.value>
4 <option value="北京">北京</option>
5 <option value="上海">上海</option>
6 <option value="武汉">武汉</option>
7 <option value="天津">天津</option>
8 <option value="大连">大连</option>
9 </select>
10 <input tppe=text name=text1 value=hello>
11 </form>
12
13
1 <body onload=document.form1.text1.value='页面加载完毕' onunload=alert('再见,欢迎再来')>
2 <form name=form1>
3 <input type=text name=text1 value="页面正在加载 ……">
4 </form>
2 <form name=form1>
3 <input type=text name=text1 value="页面正在加载 ……">
4 </form>