对一些主要事件列举一哈:
1.首先是onload、onunload方法:
乍一看这貌似是页面全加载完再执行,如下面的代码
<!DOCTYPE html>
<html>
<head>
<script>
function load()
{
alert("蛤蛤蛤");
}
</script>
</head>
<body οnlοad="load()">
<h1>Hello你个头!</h1>
</body>
</html>
此时,确实是加载完页面再执行body标签里onload。
但是有这样的情况,将window.onload写在js里面,如下面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled</title>
<script type="text/javascript">
function why()
{
alert("蛤蛤蛤");
}
window.οnlοad=why();
</script>
</head>
<body>
你看不见我
</body>
</html>
此时不再是加载完执行,而是读head时执行。
可改成:
window.οnlοad=why;//或者
window.οnlοad=function(){why();} //相当于用匿名函数把函数藏起来
onunload方法自然就是关闭网页时事件发生,情况与onload类似。
2.onclick鼠标单击事件,onmouseover鼠标经过事件,onmouseout鼠标移开事件,这在我之前的 HTML_基础_菜单栏这篇文章中有演示。
一个小例子:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>单击事件 </title>
<script type="text/javascript">
function openwin(){
window.open('http://www.baidu.com','_blank','height=600,width=400,top=100,toolbar=no,left=0,menubar=no,scrollbars=no,status=no');}
</script>
</head>
<body>
<form>
<input type="button" type="button" value="蛤蛤蛤" οnclick="openwin()">
</form>
</body>
</html>
3.onfocus 光标聚焦事件/onblur 失焦事件
指光标点击到相关区域时触发事件,如文本框的点击:
<input name="text" type="text" value="请输入文字" οnfοcus="fun()">
onblur事件若焦点改变则触发。
4.onselect内容选中事件、onchange内容改变事件
对一个文本框textarea操作:
<textarea οnselect="message()" οnchange="message()" name="haha" cols="60" rows="5">请写入个人简介,不少于200字!</textarea>
ps:cols行宽,rows列宽