整理Javascript事件响应学习笔记
什么是事件
JavaScript创建动态页面。事件是可以被JavaScript侦测到的行为。网页中的每个元素都可以产生某些可以触发JavaScript函数或程序的事件。
1、鼠标单击事件(onclick)
onclick是鼠标单击事件,当在网页上单击鼠标时,就会发生该事件。同时onclick事件调用的程序块就会被执行,通常与按钮一起使用。
例:我们单击按钮时,触发onclick事件,并调用两个数和的函数add2()。
<html>
<head>
<scripttype="text/javascript">
functionadd2(){
varnuma,numb,sum;
numa=6;
numb=8;
sum=numa+numb;
document.write("两数和为:"+sum);}
</script>
</head>
<body>
<form>
<inputname="button"type="button"value="点击提交"onclick="add2()"/>
</form>
</body>
</html>
注意:在网页中,如使用事件,就在该元素中设置事件属性。
2、鼠标经过事件(onmouseover)
鼠标经过事件,当鼠标移到一个对象上时,该对象就触发onmouseover事件,并执行onmouseover事件调用的程序。
当鼠标经过"确定"按钮后,调用message()函数,弹出消息对话框。
<!DOCTYPEHTML>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>鼠标经过事件</title>
<scripttype="text/javascript">
functionmessage(){
confirm("请输入密码后,再单击确定!");}
</script>
</head>
<body>
<form>
密码:
<inputname="password"type="password">
<inputname="确定"type="button"value="确定"onmouseover="message()"/>
</form>
</body>
</html>
3、鼠标移开事件(onmouseout)
鼠标移开事件,当鼠标移开当前对象时,执行onmouseout调用的程序。
当鼠标移开"点击我"的按钮时,调用message()函数,弹出消息对话框。
<!DOCTYPEHTML>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>鼠标移开事件</title>
<scripttype="text/javascript">
functionmessage(){
alert("不要移开,点击后进行慕课网!");}
</script>
</head>
<body>
<form>
<ahref="http://www.imooc.com"onmouseout="message()">点击我</a>
</form>
</body>
</html>
4、光标聚焦事件(onfocus)
当网页中的对象获得聚点时,执行onfocus调用的程序就会被执行。
当下拉列表得到焦点时,调用message()函数,就弹出对话框“"请选择,您现在的职业!”。
<!DOCTYPEHTML>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>光标聚焦事件</title>
<scripttype="text/javascript">
functionmessage(){
alert("请选择,您现在的职业!");
}
</script>
</head>
<body>
请选择您的职业:<br>
<form>
<selectname="career"onfocus="message()">
<option>学生</option>
<option>教师</option>
<option>工程师</option>
<option>演员</option>
<option>会计</option>
</select>
</form>
</body>
</html>
5、失焦事件(onblur)
onblur事件与onfocus是相对事件,当光标离开当前获得聚焦对象的时候,触发onblur事件,同时执行被调用的程序。
<!DOCTYPEHTML>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8">
<title>失焦事件</title>
<scripttype="text/javascript">
functionmessage(){
alert("请确定已输入密码后,在移开!");}
</script>
</head>
<body>
<form>
用户:
<inputname="username"type="text"value="请输入用户名!">
密码:
<inputname="password"type="text"value="请输入密码!"onblur="message()">
</form>
</body>
</html>
6、内容选中事件(onselect)
选中事件,当文本框或者文本域中的文字被选中时,触发onselect事件,同时调用的程序就会被执行。
当选中个人简介文本框中文字时,触发onselect事件,并弹出对话框。
<!DOCTYPEHTML>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8">
<title>内容选中事件</title>
<scripttype="text/javascript">
functionmessage(){
alert("您触发了选中事件!");}
</script>
</head>
<body>
<form>
个人简介:<br>
<textareaname="summary"cols="60"rows="5"onselect="message()">请写入个人简介,不少于200字!</textarea>
</form>
</body>
</html>
7、文本框内容改变事件(onchange)
通过改变文本框的内容来触发onchange事件,同时执行被调用的程序。
<!DOCTYPEHTML>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8">
<title>文本框内容改变事件</title>
<scripttype="text/javascript">
functionmessage(){
alert("您改变了文本内容!");}
</script>
</head>
<body>
<form>
个人简介:<br>
<textareaname="summary"cols="60"rows="5"onchange="message()">请写入个人简介,不少于200字!</textarea>
</form>
</body>
</html>
8、加载事件(onload)
事件会在页面加载完成后,立即发生,同时执行被调用的程序。
注意:1.加载页面时,触发onload事件,事件写在<body>标签内。
2.此节的加载页面,可理解为打开一个新页面时。
如下代码,当加载一个新页面时,弹出对话框“加载中,请稍等…”。
<!DOCTYPEHTML>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8">
<title>加载事件</title>
<scripttype="text/javascript">
functionmessage(){
alert("加载中,请稍等…");}
</script>
</head>
<bodyonload="message()">
欢迎学习JavaScript。
</body>
</html>
9、卸载事件(onunload)
当用户退出页面时(页面关闭、页面刷新等),触发onUnload事件,同时执行被调用的程序。
注意:不同浏览器对onunload事件支持不同。
<!DOCTYPEHTML>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8">
<title>卸载事件</title>
<scripttype="text/javascript">
window.onunload=onunload_message;
functiononunload_message(){
alert("您确定离开该网页吗?");
}
</script>
</head>
<bodyonunload="onunload_message">
欢迎学习JavaScript。
</body>
</html>
测试结果发现只有在IE浏览器里执行,其他浏览器不执行。
以上就是关于Javascript事件响应的九种状态,希望对大家的学习有所帮助。