JavaScript使用经典
第一章javascript简介
1.在地址栏输入javascript语句
Javascript:Document.write("显示文字")
2.将javascript嵌入 HTML文档
<script language=javascript>
document.bgColor="blue"
</script>
第二章 使用变量和数组
1.声明变量
<script language=javascripe>
Var answer1,answer2,answer3,answer4;
answer1=9;
answer2=2.5
answer3="Milkey May"
answer4=true
</script>
2.使用整数
<script language=javascript>
var decimalNum,hexadecimalNum,octalNum
decimalNum=24
hexadecimalNum=0x24
octalNum=024
document.write("显示十进制数:"+ decimalNum+"<br>")
document.write("显示十六进制数:"+ hexadecimalNum +"<br>")
document.write("显示八进制数:"+ octalNum +"<br>")
</script>
3.使用浮点数
<script language=javascript>
num3=0.0000123456
num4=6.0254e3-4
document.write("浮点数1:"+num1+"<br>")
document.write("浮点数2:"+num2+"<br>")
document.write("浮点数3:"+num3+"<br>")
document.write("浮点数4:"+num4+"<br>")
</script>
4.使用布尔值
<script language=javascript>
var answer1,answer2
answer1=true
answer2=false
document.write("显示布尔1:"+answer1+"<br>")
document.write("显示布尔2:"+answer2+"<br>")
</script>
5.使用字符串
<script language=javascript>
var str1,str2
str1="fdsgdg dsfdsf china"
str2="武汉市广播电视大学"
document.write("显示字符串1:"+str1+"<br>")
document.write("显示字符串2:"+str2+"<br>")
</script>
6.确定变量类型
<script>
var answer1,answer2,answer3,answer4
answer1=9
answer2=2.5
answer3="milky may"
answer4=true
document.write("变量1的类型是:"+typeof answer1 +"<br>")
document.write("变量2的类型是:"+typeof answer2 +"<br>")
document.write("变量3的类型是:"+typeof answer3 +"<br>")
document.write("变量4的类型是:"+typeof answer4 +"<br>")
</script>
7.将字符串转换成数字
<script>
var str1="31 days in january"
var int1=parseInt(str1)
document.write("str1的数据类型是 :"+typeof str1+"<br>")
document.write("int1的数据类型是 :"+typeof int1+"<br>")
</script>
8.将数字转换成字符串
<script>
var int1=256
var str1=""+int1
document.write("str1的数据类型是 :"+typeof str1+"<br>")
document.write("int1的数据类型是 :"+typeof int1+"<br>")
</script>
9.声明数组
<script>
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])
</script>
10.确定数组元素的个数
<script>
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]+"<br>")
document.write("数组的元素个数是"+array.length)
</script>
11.将数组转换为字符串
<script>
array=new Array()
array[0]="dark"
array[1]="apple"
array[2]="nebula"
array[3]="water"
str1=array.join()
str2=array.join(" ")
document.write(str1+"<br>")
document.write(str2)
</script>
12.对数组排序
<script>
array=new Array()
array[0]="dark"
array[1]="apple"
array[2]="nebula"
array[3]="water"
str1=array.sort()
document.write(str1+"<br>")
</script>
第三章 创建表达式
1.使用算术运算符
<script>
var1=12
var2=10
varadd=var1+var2
varsub=var1-var2
varmult=var1*var2
vardiv=var1/var2
varmod=var1%var2
document.write("数据1是:"+var1+"<br>")
document.write("数据2是:"+var2+"<br>")
document.write("数据相加是:"+varadd+"<br>")
document.write("数据相减是:"+varsub+"<br>")
document.write("数据相乘是:"+varmult+"<br>")
document.write("数据相除是:"+vardiv+"<br>")
document.write("数据相除取余数是:"+varmod+"<br>")
</script>
2.递增变量和递减变量
<script>
days=1
document.write("输出变量"+days+"<br>")
days++
document.write("递增后变量变为:"+days)
</script>
3.创建比较表达式
<script>
daysofmonth=28
if(daysofmonth==28)
month="february"
document.write("days of month:"+daysofmonth+"<br>")
document.write("month:"+month)
</script>
4.创建逻辑表达式
<script>
dayofmonth=28
if(dayofmonth==28 || dayofmonth==29)
month="february"
document.write("days of month:"+dayofmonth+"<br>")
document.write("month:"+month)
</script>
5.使用条件运算符
<script language="javascript">
stomach="hungry";
time="5:00";
(stomach=="hungry"&&time=="5:00") ? eat = "dinner":eat="a snack";
document.write("输出结果"+eat);
</script>
6.识别数字
<script>
var1=24;
(isNaN(var1))?document.write("变量var1"+var1+"不是数字"):Document.write("变量var1"+var1+"是数字")
</script>
第四章 控制程序流程
1.使用IF –Else语句
<script>
month="december"
date=25
if(month=="december" && date==25)
document.write("今天是圣诞节,商店关门")
else
document.write("欢迎,您来商店购物")
</script>
2.使用for 循环
<script>
for (count=1;count<=10;count++)
document.write("输出第"+count+"句"+"<br>")
</script>
3.使用while循环
<script>
count=1
while(count<=15){
document.write("输出第"+count+"句" +"<br>")
count++}
</script>
4.中断循环
<script>
count=1
while(count<=15){
count++
if(count==8)
break;
document.write("输出第"+count+"句"+"<br>")}
</script>
5.继续循环
<script>
count=1
while(count<=15){
count++
if(count==8)
continue;
document.write("输出第"+count+"句"+"<br>")}
</script>
6.使用javascript定时器
<script>
function rabbit()
{document.write("输出语句")
}
</script>
<body _disibledevent=document.cookieindexOf(";");
document.writeln("your name is:"+document.cookie.substring(namestart+1,nameend)+",br>")
}
}
</script>