1、捕捉回车键
<!DOCTYPE html>
<html>
<head>
<title>捕捉回车键</title>
</head>
<body>
<script type="text/javascript">
/*
回车键的键值为13
esc键的键值为27
*/
window.onload=function(){
document.getElementById("nn").onkeydown=function(event){//event是自定义变量,且最多只能放一个,不写变量也能运行;写上变量是接受事件对象
//获取键值
//对于“键盘事件”来说,都有keyCode属性来获取键值
alert(event.keyCode);//结果为按下键盘的键值
if(event.keyCode==13){
alert("正在加载");
}
}
}
</script>
<input type="text" id="nn"/>
</body>
</html>
2、void运算符
<!DOCTYPE html>
<html>
<head>
<title>void运算符</title>
</head>
<body>
<br><!--换行-->
<a href="javascript:void(0)" onclick="window.alert('cc')">
<!--
void运算符的语法:void(表达式)
表达式可以为任意的值,但不能不写
运算原理:执行表达式,但不返回任何结果。(null、空字符串、undefined都不会返回)
javascript:void(0)
其中javascript:作用是告诉浏览器后面是一段JS代码。
以下程序的javascript:是不能省略的。
如果省略javascript:系统将默认void(0)为返回路径
-->
保留超链接的样式,同时用户点击时运行一段js代码,但页面不能跳转
</a>
</body>
</html>
3、js控制语句
<!DOCTYPE html>
<html>
<head>
<title>js的控制语句</title>
</head>
<body>
<script type="text/javascript">
//for...in...
//创建数组
var y=[123,"aaa",undefined];//用中括号,元素类型随意,元素个数随意
/*
回忆java创建数组
public class Y{
public static void main(String[] args){
String[] i={"a","s"};
String j=new String[2];
}
}
*/
for(var u in y){
alert(u);//u为数组的下标
alert(y[u]);//遍历数组
}
tt=function(a,b){
this.a=a;
this.b=b;
}
var h=new tt("kk",13);
alert(h.a);
alert(h.b);
//也可以写成
alert(h["a"]);
alert(h["b"]);
//用for...in..
for(var i in h){
alert(i);//结果为属性名,string类型
alert(h[i]);
}
//with方法
with(h){
alert(a+","+b)//这里的a等于h.a,b等于h.b
}
</script>
</body>
</html>