系列首篇:javaccript:
网景公司开发,通用,跨平台,基于对象和驱动的解释型脚本编程语言(javascript是动态编译的)
应用:客户端的验证,实时显示(如markdown),动画效果,窗口应用
基础部分:
数据类型:
- 数值型(并不区分整数型和浮点型,所有数值均是由浮点型表示)
- 字符串型(由unicode字符、数字、标点符号组成的有序序列)
包含在单引号或者双引号之间
‘asdfasd’
“asdfasdf”
‘name = “asd”’
“call me ‘asdf’” - 布尔型(两个值)
- 特殊字符型
转义字符(\b \n \t \r \ …)
未定义值(undefined)
空值(null)
变量(统一用var声明,用的时候在赋值)
- 表达式(与c相同,多了一个‘===’)
- 基本语句(与c相同)
<html>
<head>
<script>
function check(form){
//用于验证用户名是否为空
if(form.username.value == ""){
alert("请输入用户名");
form.username.focus();
return(false);
}
}
</script>
</head>
<body>
<form name = "form1">
username:
<input type = "text" name = "username" onblur = "check1(form1)"/>
<!-- 失去焦点调用-->
<br />
password:
<input type = "text" name = "password" />
</form>
</body>
</html>
- 函数
- 函数的定义
<script language = "javascript">
function functionname(parameter){
statements;
}
</script>
- 函数的调用
1、简单的函数调用
<html>
<head>
<meta http-equiv = "Content-Type" content="text/html; charset=utf-8">
<title>简单函数调用</title>
<script type = "text/javascript">
function print(statement){
alert(statement);
}
</script>
</head>
<body>
<script type = "text/javascript">
print("asdfasd");
</script>
</body>
</html>
2、在事件响应中调用函数
<html>
<head>
<script>
function check(form){
if(form.username.value == ""){
alert("请输入用户名");
form.username.focus();
return(false);
}
}
</script>
</head>
<body>
<form name = "form1">
username:
<input type = "text" name = "username" onblur = "check1(form1)"/>
<br />
password:
<input type = "text" name = "password" />
</form>
</body>
</html>
3、通过链接调用函数
<html>
<head>
<meta http-equiv = "Content-Type" content="text/html; charset = UTF-8">
<title>简单函数调用</title>
<script type = "text/javascript">
function print(){
alert("aaaa");
}
</script>
</head>
<body>
<a href = "javascript:print()">aaa</a>
</body>
</html>