JavaScript 学习笔记(二)
程序结构:JavaScript脚本语言只提供了两种结构:条件结构, 循环结构
If语句:
<html >
<head>
<title>IF语句的使用</title>
</head>
<body>
<script language="javascript">
function rec(form)
{
var a=form.recshortth.value;
{
if(a>60)
alert("及格");
else
alert("不及格");
}
}
</script>
<form>
<input type="text" name="recshortth"><br>
<input name="button" type="button" onclick="rec(this.form)" value="显示">
</form>
</body>
</html>
<head>
<title>IF语句的使用</title>
</head>
<body>
<script language="javascript">
function rec(form)
{
var a=form.recshortth.value;
{
if(a>60)
alert("及格");
else
alert("不及格");
}
}
</script>
<form>
<input type="text" name="recshortth"><br>
<input name="button" type="button" onclick="rec(this.form)" value="显示">
</form>
</body>
</html>
For语句:
<html>
<head>
<title>For语句循环</title>
</head>
<body>
<script language="javascript">
var i=1
for(i=1;i<=4;i++)
{
document.write("<h",i,">欢迎大家一起来学习JavaScript!</h",i,">");
}
</script>
</body>
</html>
<head>
<title>For语句循环</title>
</head>
<body>
<script language="javascript">
var i=1
for(i=1;i<=4;i++)
{
document.write("<h",i,">欢迎大家一起来学习JavaScript!</h",i,">");
}
</script>
</body>
</html>
Do While语法
<html>
<head>
<title>Do While使用</title>
</head>
<body>
<script language="javascript">
var i=1;
do
{ document.write("<h",i,">欢迎大家一起来学习JavaScript!</h",i,">");
i++;}
while(i<1)
</script>
</body>
</html>
<head>
<title>Do While使用</title>
</head>
<body>
<script language="javascript">
var i=1;
do
{ document.write("<h",i,">欢迎大家一起来学习JavaScript!</h",i,">");
i++;}
while(i<1)
</script>
</body>
</html>
Switch语句:
<html>
<head>
<title>Switch语句的使用</title>
</head>
<body>
<script language="javascript">
function rec(form)
{var a=form.recshortth.value;
switch(a)
{case '90':
{document.write("优秀");
break;}
case '80':
{document.write("良好");
break;}
case '70':
{document.write("中等");
break;}
case '60':
{document.write("及格");
break;}
default:
{document.write("不及格");
break;}
}
}
</script>
<form>
<input type="text" name="recshortth"><br>
<input type="button" name="button" onclick="rec(this.form)" value="显示">
</form>
</body>
</html>
<head>
<title>Switch语句的使用</title>
</head>
<body>
<script language="javascript">
function rec(form)
{var a=form.recshortth.value;
switch(a)
{case '90':
{document.write("优秀");
break;}
case '80':
{document.write("良好");
break;}
case '70':
{document.write("中等");
break;}
case '60':
{document.write("及格");
break;}
default:
{document.write("不及格");
break;}
}
}
</script>
<form>
<input type="text" name="recshortth"><br>
<input type="button" name="button" onclick="rec(this.form)" value="显示">
</form>
</body>
</html>
函数:
在JavaScript脚本语言中, 是使用Function来定义函数的, 函数的结构分为:有参函数和无参函数.
1. 有参函数
Function 函数名(参数1,参数2...)
2. 无参函数
Function 函数名()
JavaScript常用元素: 常量, 变量, 运算符, 函数, 对象, 事件
实例:
<html>
<head>
<title>这是第一个JavaScript程序</title>
</head>
<body>
<script language="javascript">
function rec()
{
alert("你好!欢迎大家一起来学习JavaScript!")
}
</script>
<form>
<input name="button" type="button" onclick="rec()" value="运行程序">
<br>
</form>
</body>
</html>
<head>
<title>这是第一个JavaScript程序</title>
</head>
<body>
<script language="javascript">
function rec()
{
alert("你好!欢迎大家一起来学习JavaScript!")
}
</script>
<form>
<input name="button" type="button" onclick="rec()" value="运行程序">
<br>
</form>
</body>
</html>