第三章 創(chuàng)建表達(dá)式
1.使用算術(shù)運(yùn)算符
<script> var1=12 var2=10 varadd=var1+var2 varsub=var1-var2 varmult=var1*var2 vardiv=var1/var2 varmod=var1%var2 document.write("數(shù)據(jù)1是:"+var1+"<br>") document.write("數(shù)據(jù)2是:"+var2+"<br>") document.write("數(shù)據(jù)相加是:"+varadd+"<br>") document.write("數(shù)據(jù)相減是:"+varsub+"<br>") document.write("數(shù)據(jù)相乘是:"+varmult+"<br>") document.write("數(shù)據(jù)相除是:"+vardiv+"<br>") document.write("數(shù)據(jù)相除取余數(shù)是:"+varmod+"<br>") </script>
2.遞增變量和遞減變量
<script> days=1 document.write("輸出變量"+days+"<br>") days++ document.write("遞增后變量變?yōu)椋?+days) </script>
3.創(chuàng)建比較表達(dá)式
<script> daysofmonth=28 if(daysofmonth==28) month="february" document.write("days of month:"+daysofmonth+"<br>") document.write("month:"+month) </script>
4.創(chuàng)建邏輯表達(dá)式
<script> dayofmonth=28 if(dayofmonth==28 || dayofmonth==29) month="february" document.write("days of month:"+dayofmonth+"<br>") document.write("month:"+month) </script>
5.使用條件運(yùn)算符
<script language="javascript"> stomach="hungry"; time="5:00"; (stomach=="hungry"&&time=="5:00") ? eat = "dinner":eat="a snack"; document.write("輸出結(jié)果"+eat); </script>
6.識(shí)別數(shù)字
<script> var1=24; (isNaN(var1))?document.write("變量var1"+var1+"不是數(shù)字"):Document.write("變量var1"+var1+"是數(shù)字") </script>
第四章 控制程序流程
1.使用IF –Else語句
<script> month="december" date=25 if(month=="december" && date==25) document.write("今天是圣誕節(jié),商店關(guān)門") else document.write("歡迎,您來商店購物") </script>
2.使用for 循環(huán)
<script> for (count=1;count<=10;count++) document.write("輸出第"+count+"句"+"<br>") </script>
3.使用while循環(huán)
<script> count=1 while(count<=15){ document.write("輸出第"+count+"句" +"<br>") count++} </script>
4.中斷循環(huán)
<script> count=1 while(count<=15){ count++ if(count==8) break; document.write("輸出第"+count+"句"+"<br>")} </script>
5.繼續(xù)循環(huán)
<script> count=1 while(count<=15){ count++ if(count==8) continue; document.write("輸出第"+count+"句"+"<br>")} </script>
6.使用javascript定時(shí)器
<script> function rabbit() {document.write("輸出語句") } </script> <body onload=window.setTimeout(rabbit(),5000)>
7.設(shè)置定期間隔
<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.清除超時(shí)和間隔
<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=" 清除超時(shí)和間隔" onclick=clearInterval(stop)> </form>
出處:藍(lán)色理想
責(zé)任編輯:moby
上一頁 事半功倍之Javascript [1] 下一頁 事半功倍之Javascript [3]
◎進(jìn)入論壇網(wǎng)頁制作、網(wǎng)站綜合版塊參加討論
|