javascript
本文由經典論壇 鄧永炎 整理
清華大學出版的《事半功倍系列 javascript》,本人照著書敲出來的,有些翻譯了一下.前幾年看了一下,最近無事,重新翻了翻,很有協助.本書應該有光碟片的,但學校的書,光碟片不知在哪.希望對你學 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>
var num1,num2,num3,num4
num1=1234567890000.0
num2=5.14e23
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 onload=window.setTimeout(rabbit(),5000)>
7.設定定期間隔
<script>
window.setInterval("document.form1.text2.value=document.form1.text1.value",3000)
</script>
<form name=form1>
<input type=text name=text1><br>
<input type=text name=text2><br>
</form>
8.清除逾時和間隔
<script>
stop=window.setInterval("document.form1.text2.value=document.form1.text1.value",300)
</script>
<form name=form1>
<input type=text name=text1><br>
<input type=text name=text2><br>
<input type=button name=button1 value=" 清除逾時和間隔" onclick=clearInterval(stop)>
</form>