JS标签的各种事件的举例
1.鼠标单击事件( onclick )
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>单击事件 </title> 6 <script type="text/javascript"> 7 function openwin(){ 8 window.open('http://www.cnblogs.com/lonecloud/','_blank','height=600,width=400,top=100,toolbar=no,left=0,menubar=no,scrollbars=no,status=no');} 9 </script> 10 </head> 11 <body> 12 <form> 13 <input name="点击我" type="button" value="点击我" onClick="openwin()"/> 14 </form> 15 </body> 16 </html>
2.鼠标经过事件(onmouseover)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title> 鼠标经过事件 </title> 6 <script type="text/javascript"> 7 function message(){ 8 confirm("请输入密码后,再单击确定!"); } 9 </script> 10 </head> 11 <body> 12 <form> 13 密码:<input name="password" type="password" > 14 <input name="确定" type="button" value="确定"onmouseover="message()"/> 15 </form> 16 </body> 17 </html>
3.鼠标移开事件(onmouseout)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>鼠标移开事件 </title> 6 <script type="text/javascript"> 7 function message(){ 8 alert("不要移开,点击后进入我的博客!"); } 9 </script> 10 </head> 11 <body> 12 <form> 13 <a href="http://www.imooc.com" onmouseout="message()">点击我</a> 14 </form> 15 </body> 16 </html>
4.光标聚焦事件(onfocus)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title> 光标聚焦事件 </title> 6 <script type="text/javascript"> 7 function message(){ 8 alert("请选择,您现在的职业!"); 9 } 10 </script> 11 </head> 12 <body> 13 请选择您的职业:<br> 14 <form> 15 <select name="career" onfocus="message()"> 16 <option>学生</option> 17 <option>教师</option> 18 <option>工程师</option> 19 <option>演员</option> 20 <option>会计</option> 21 </select> 22 </form> 23 </body> 24 </html>
5.失焦事件(onblur)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title> 失焦事件 </title> 6 <script type="text/javascript"> 7 function message(){ 8 alert("请确定已输入密码后,在移开!"); } 9 </script> 10 </head> 11 <body> 12 <form> 13 用户:<input name="username" type="text" value="请输入用户名!" onblur="message()" > 14 密码:<input name="password" type="text" value="请输入密码!" > 15 </form> 16 </body> 17 </html>
6.内容选中事件(onselect)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title> 内容选中事件 </title> 6 <script type="text/javascript"> 7 function message(){ 8 alert("您触发了选中事件!"); } 9 </script> 10 </head> 11 <body> 12 <form> 13 个人简介:<br> 14 <textarea name="summary" cols="60" rows="5" onselect="message()">请写入个人简介,不少于200字!</textarea> 15 </form> 16 </body> 17 </html>
7.文本框内容改变事件(onchange)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title> 文本框内容改变事件 </title> 6 <script type="text/javascript"> 7 function message(){ 8 alert("您改变了文本内容!"); } 9 </script> 10 </head> 11 <body> 12 <form> 13 个人简介:<br> 14 <textarea name="summary" cols="60" rows="5"onChange="message()">请写入个人简介,不少于200字!</textarea> 15 </form> 16 </body> 17 </html>
8.加载事件(onload)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title> 加载事件 </title> 6 <script type="text/javascript"> 7 function message(){ 8 alert("加载中,请稍等…"); } 9 </script> 10 </head> 11 <body onload="message()"> 12 欢迎学习JavaScript。 13 </body> 14 </html>
9.卸载事件(onunload)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title> 卸载事件 </title> 6 <script type="text/javascript"> 7 window.onunload = onunload_message; 8 function onunload_message(){ 9 alert("您确定离开该网页吗?"); 10 } 11 </script> 12 </head> 13 <body onunload="onunload_message()"> 14 欢迎学习JavaScript。 15 </body> 16 </html>