第三章 创建表达式
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 οnlοad=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=" 清除超时和间隔" οnclick=clearInterval(stop)>
</form>