1.1 JavaScript的基本语句
1.1.1 条件语句
条件语句的语法及其示例见下表。
类型
语法
案例
壹
if (logical expression)
statement1;
if (count>4)
count++;
贰
if (logical expression)
statement1;
else
statement2;
if (count>10)
count=document.frm.text.value;
else
count=document.frm.name.value;
叁
if (logical expression1)
statement1;
else if (logical expression2)
statement2;
else if (logical expression3)
statement3;
……
var age=document.frm.age.value;
if (age>=0 && age<=10)
age=document.frm.text_01.value;
else if (age>=11 && age<=20)
age=document.frm.text_02.value;
else if (age>=21 && age<=30)
age=document.frm.text_03.value;
1.1.2 条件语句应用示例
<html>
<head>
<title>Welcome to hfcec...</title>
</head>
<body>
<form name="tsinghua">
请输入您的年龄:
<input type="text" name="university" value="">
<p>
<input type="button" value="Please Click Me!" name="cmdok" onclick="cmdok_onclick()">
<input type="reset" value="Clear All Data of Form Object!">
<p>
<hr size="1" color="orange">
<p>
<input type="text" name="guoyongcan" value="" size="60">
<script language="javascript">
<!--
function cmdok_onclick()
{
var age=parseInt(document.tsinghua.university.value);
if (age<0 || age>100)
alert("请输入正确的年龄!");
else if (age<=10)
document.tsinghua.guoyongcan.value="您输入的年龄是"+age+","+"您尚处于婴儿时期!";
else if (age<=20)
document.tsinghua.guoyongcan.value="您输入的年龄是"+age+","+"您即将步入成年时期!";
else if (age<=30)
document.tsinghua.guoyongcan.value="您输入的年龄是"+age+","+"您是不是该结婚了?!";
else if (age<=40)
document.tsinghua.guoyongcan.value="您输入的年龄是"+age+","+"您现在应该是爱情事业双丰收了罢?!";
else if (age<=50)
document.tsinghua.guoyongcan.value="您输入的年龄是"+age+","+"您老了,该退休了,IT已经不适合你啦!";
else if (age<=100)
document.tsinghua.guoyongcan.value="您输入的年龄是"+age+","+"您好好在家看小孩吧!";
else
alert("您输入的是非法字符,请重新输入正确的年龄,谢谢!");
}
//-->
</script>
</form>
</body>
</html>
1.3 循环及其相关语句
循环语句的语法及其示例见下表。
类型
语法
案例
壹
for (start value;goal value;step value)
{
statement;
}
for (i=1;i<=9;i++)
{
var m=i*5;
}
贰
while (expression)
{
statement;
}
while (i<=3 && i>=1 )
{
var count=count*i;
document.write(count);
}
叁
do
{
statement;
}
while (expression);
do
{
var count=count*i;
document.write(count);
}
while (i<=3 && i>=1);
肆
for (expression in form object)
{
statement;
}
var gyc;
for (gyc in document.tsinghua.university)
{
document.write(gyc,"<br>");
}
伍
break;(跳出循环)
break;
陆
continue;(跳转至循环开始的地方)
continue;
1.1.4 循环语句应用示例
<html>
<head><title>Welcome to hfcec...</title></head>
<body>
<script language="javascript">
<!--
document.write("以下脚本能输出一个简单的乘法口诀表");
document.write("<hr size='1' color='#ffooo'>");
document.write("<p>");
for (i=1;i<=9;i++){
for (j=1;j<=9;j++){
var num=i*j;
if (num<10) {document.write(i,"*",j,"=",num," ");}
else
document.write(i,"*",j,"=",num," ");
}
document.write("<br>");
}
//-->
</script>
</body>
</html>
<html>
<head><title>Welcome to hfcec...</title></head>
<body>
<form name="tsinghua">
<input type="text" name="university" value="guoyongcan...">
</form>
<p>
<script language="javascript">
<!--
var gyc;
for (gyc in document.tsinghua.university)
{
document.write(gyc,"<br>");
}
//-->
</script>
</body>
</html>
1.2 JavaScript中的函数
1.2.1 函数的定义方法
function function_name(parameters)
{
statement;
...... ;
}
1.2.2 函数的返回值
函数的返回值是指函数执行完毕以后的结果,在JavaScript中用return语句获取函数的返回值,其说明示例如下代码所示。
<html><head><title>Welcome to hfcec...</title></head>
<body>
<script language="javascript">
<!--
var num1=3;
var num2=5;
function add(variable1,variable2)
//定义一个加法函数
{
return variable1+variable2;
}
function num(variable1,variable2)
//定义一个乘法函数
{
return variable1*variable2;
}
document.write("变量的初始值是:",num1,",",num2,"<br>");
document.write("调用加法函数的输出结果是:",add(num1,num2),"(3+5)","<br>");
document.write("调用乘法函数的输出结果是:",num(num1,num2),"(3×5)","<br>");
-->
</script>
</body>
</html>