关于javascript事件响应的基础语法总结(必看篇)
1、onclick是鼠标单击事件,当在网页上单击鼠标时,就会发生该事件。同时onclick事件调用的程序块就会被执行,通常与按钮一起使用
//例子
<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>
//单击按钮时,触发onclick事件,并调用两个数和的函数add2()
2、onmouseover是鼠标经过事件,当鼠标移到一个对象上时,该对象就触发onmouseover事件,并执行onmouseover事件调用的程序
<!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>
//鼠标经过"确定"按钮时,触发onmouseover事件,调用函数message(),弹出消息框
3、onmouseout鼠标移开事件,当鼠标移开当前对象时,执行onmouseout调用的程序。
<!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="https://www.nhooo.com"onmouseout="message()">点击我</a>
</form>
</body>
</html>
//当把鼠标移动到"点击我"按钮上,然后再移开时,触发onmouseout事件,调用函数message()
4、onfocus事件是当网页中的对象获得聚点时,执行onfocus调用的程序就会被执行。
<!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>
//当下拉列表得到焦点时,调用message()函数,就弹出对话框“"请选择,您现在的职业!”。
5、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="请输入用户名!"onblur="message()">
密码:<inputname="password"type="text"value="请输入密码!">
</form>
</body>
</html>
//当光标从密码文本框离开时,触发onblur事件,调用message()函数。
6、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>
//当选中个人简介文本框中文字时(相当于复制粘贴中的复制这个行为就是选中文本),触发onselect事件,并弹出对话框。
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">请写入个人简介,不少于200字!</textarea>
</form>
</body>
</html>
//改变文本框内容时,鼠标的聚点还在文本框的时候并不会弹出对话框,但当改变完内容后,鼠标移动到别处(鼠标聚点离开文本框的时候)便会立刻调用message()函数,弹出对话框“您改变了文本内容!”
8、onload加载事件会在页面加载完成后,立即发生,同时执行被调用的程序。
注意:加载页面时,触发onload事件,事件写在<body>标签内。
<!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>
//加载页面时,弹出对话框“加载中,请稍等…”。
9、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>
<body>
欢迎学习JavaScript。
</body>
</html>
//当退出页面时,弹出对话框“您确定离开该网页吗?”。
以上就是小编为大家带来的关于javascript事件响应的基础语法总结(必看篇)全部内容了,希望大家多多支持毛票票~