接着上篇博客:JavaScript基础学习笔记(一)
本文主要是对JavaScript的一些基础部分的使用的记录,包括运算符、控制结构、Break和continue、错误处理以及表单验证的介绍。
1.运算符
• 运算符 =
用于赋值。
• 运算符 +
对于数值型数据+用于加值。
对于字符串类型数据或数字与字符串相加,结果都为字符串。
例子如下:
x=5+5;
document.write(x);
x="5"+"5";
document.write(x);
x=5+"5";
document.write(x);
x="5"+5;
document.write(x);
输出结果为:
10 //数值 10
55 //字符串 55
55 //字符串 55
55 //字符串 55
• 算术运算符
算术运算符用于执行变量与/或值之间的算术运算。
给定 y=5,下面的表格解释了这些算术运算符:
• 赋值运算符
给定 x=10 和 y=5,下面的表格解释了赋值运算符:
•比较运算符
给定 x=5,下面的表格解释了比较运算符:
•逻辑运算符
给定 x=6 以及 y=3,下表解释了逻辑运算符:
•条件运算符
variablename=(表达式)?取值1:取值2
如果表达式为真,则向变量 variablename为“取值1”,否则为“取值2”。
2.控制结构
三大基础控制结构包括:顺序结构、条件结构、循环结构,这里主要来说一下条件语句和循环语句的使用。
条件语句
条件语句主要有if、if…else、if…else if…else和switch语句
- if 语句 - 只有当指定条件为 true 时,使用该语句来执行代码
if (条件)
{
只有当条件为 true 时执行的代码
}
- if…else 语句 - 当条件为 true 时执行代码,当条件为 false 时执行其他代码
if (条件)
{
当条件为 true 时执行的代码
}
else
{
当条件不为 true 时执行的代码
}
- if…else if….else 语句 - 使用该语句来选择多个代码块之一来执行
if (条件 1)
{
当条件 1 为 true 时执行的代码
}
else if (条件 2)
{
当条件 2 为 true 时执行的代码
}
else
{
当条件 1 和 条件 2 都不为 true 时执行的代码
}
- switch 语句 - 使用该语句来选择多个代码块之一来执行
switch(n)
{
case 1:
执行代码块 1
break;
case 2:
执行代码块 2
break;
default:
n 与 case 1 和 case 2 不同时执行的代码
}
循环 语句
循环语句有for和while两种循环语句
For循环
- for - 循环代码块一定的次数
for (语句 1; 语句 2; 语句 3)
{
被执行的代码块
}
语句 1 在循环(代码块)开始前执行
语句 2 定义运行循环(代码块)的条件
语句 3 在循环(代码块)已被执行之后执行
- for/in - 循环遍历对象的属性
var person={fname:"John",lname:"Doe",age:25};
for (x in person)
{
txt=txt + person[x];
}
while 循环
- While 循环会在指定条件为真时循环执行代码块。
while (条件)
{
需要执行的代码
}
- do/while 循环
do/while 循环是 while 循环的变体。该循环会执行一次代码块,在检查条件是否为真之前,然后如果条件为真的话,就会重复这个循环。
do
{
需要执行的代码
}
while (条件);
3.Break 和 Continue 语句
- break 语句用于跳出循环体,就是说整个for循环就结束了。
- continue 用于跳过循环中的一个迭代,也就是说跳出本次循环,进行下一次的循环。
注意:
continue 语句(带有或不带标签引用)只能用在循环中。
break 语句(不带标签引用),只能用在循环或 switch 中。
通过标签引用,break 语句可用于跳出任何 JavaScript 代码块。
4.错误处理
js中的错误处理语句有Throw、Try和Catch
- throw 语句创建或抛出异常(exception)
- try 语句允许我们定义在执行时进行错误测试的代码块。
- catch 语句允许我们定义当 try 代码块发生错误时,所执行的代码块。
注意:Js语句 try 和 catch 是成对出现的。
try
{
//在这里运行代码
throw exception; //try...catch可以和throw一起使用
}
catch(err)
{
//在这里处理错误
}
5.表单验证
JavaScript 可用来在数据被送往服务器前对 HTML 表单中的这些输入数据进行验证。
被 JavaScript 验证的这些典型的表单数据有:
- 用户是否已填写表单中的必填项目?
- 用户输入的邮件地址是否合法?
- 用户是否已输入合法的日期?
- 用户是否在数据域 (numeric field) 中输入了文本?
必填(或必选)项目
下面的函数用来检查用户是否已填写表单中的必填(或必选)项目。假如必填或必选项为空,那么警告框会弹出,并且函数的返回值为 false,否则函数的返回值则为 true(意味着数据没有问题):
<html>
<head>
<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
{alert(alerttxt);return false}
else {return true}
}
}
function validate_form(thisform)
{
with (thisform)
{
if (validate_required(email,"Email must be filled out!")==false)
{email.focus();return false}
}
}
</script>
</head>
<body>
<form action="submitpage.htm" onsubmit="return validate_form(this)" method="post">
Email: <input type="text" name="email" size="30">
<input type="submit" value="Submit">
</form>
</body>
</html>
E-mail 验证
下面的函数检查输入的数据是否符合电子邮件地址的基本语法。
意思就是说,输入的数据必须包含 @ 符号和点号(.)。同时,@ 不可以是邮件地址的首字符,并且 @ 之后需有至少一个点号。
<html>
<head>
<script type="text/javascript">
function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@")
dotpos=value.lastIndexOf(".")
if (apos<1||dotpos-apos<2)
{alert(alerttxt);return false}
else {return true}
}
}
function validate_form(thisform)
{
with (thisform)
{
if (validate_email(email,"Not a valid e-mail address!")==false)
{email.focus();return false}
}
}
</script>
</head>
<body>
<form action="submitpage.htm"onsubmit="return validate_form(this);" method="post">
Email: <input type="text" name="email" size="30">
<input type="submit" value="Submit">
</form>
</body>
</html>