JavaScript中的常用的事件响应

1.鼠标单击事件(onclick)

(1)语法:οnclick="message()";

(2)作用:鼠标点击网页中的按钮,就会调用相应的程序块,通常与按钮(button)一起使用

(3)例子:

<html>
<head>
   <script type="text/javascript">
      function add2(){
        var numa,numb,sum;
        numa=6;
        numb=8;
        sum=numa+numb;
        document.write("两数和为:"+sum);  }
   </script>
</head>
<body>
   <form>
      <input name="button" type="button" value="点击提交" οnclick="add2()" />
   </form>
</body>
</html>

 

2.鼠标经过事件(onmouseover)

(1)语法:οnmοuseοver="message()";

(2)作用:鼠标经过事件,当鼠标移到一个对象上时,该对象就触发onmouseover事件,并执行onmouseover事件调用的程序。

(3)例子:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 鼠标经过事件 </title>
<script type="text/javascript">
    function message(){
      confirm("请输入密码后,再单击确定!"); }
</script>
</head>
<body>
<form>
密码:<input name="password" type="password" >
<input name="确定" type="button" value="确定" οnmοuseοver="message()"/>
</form>
</body>
</html>

 

3.鼠标移开事件(onmouseout)

(1)语法:οnmοuseοut="message()";

(2)作用:鼠标移开事件,当鼠标移开当前对象时,执行onmouseout调用的程序。

(3)例子:

<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>鼠标移开事件 </title>
<script type="text/javascript">
  function message(){
    alert("此路是我开,此树是我栽,要想从此过,只有点进来!"); }
</script>
</head>
<body>
<form>
  <a href="http://www.imooc.com" οnmοuseοut="message()">点击我</a>
</form>
</body>
</html>

 

4.光标聚焦事件(onfocus)

(1)语法:οnfοcus="message()";

(2)作用:当网页中的对象获得聚点时,执行onfocus调用的程序就会被执行。

(3)例子:

<script type="text/javascript">
    function message(){
      alert("请选择,您现在的职业!");
    }
  </script>
</head>
<body>
请选择您的职业:<br>
  <form>
    <select name="career" οnfοcus="message()"> 
      <option>学生</option> 
      <option>教师</option> 
      <option>工程师</option> 
      <option>演员</option> 
      <option>会计</option> 
    </select> 
  </form>
</body>

 

5.光标失焦事件(onblur)

(1)语法:οnblur="message()";

(2)作用:onblur事件与onfocus是相对事件,当光标离开当前获得聚焦对象的时候,触发onblur事件,同时执行被调用的程序。

(3)例子:

<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 失焦事件 </title>
<script type="text/javascript">
  function message(){
    alert("请确定已输入密码后,在移开!"); }
</script>    
</head>
<body>
  <form>
   用户:<input name="username" type="text" value="请输入用户名!" >
   密码:<input name="password" type="text" value="请输入密码!" οnblur="message()">
  </form>
</body>
</html>

 

6.内容选中事件(onselect)

(1)语法:οnselect="message()";

(2)作用:选中事件,当文本框或者文本域中的文字被选中时,触发onselect事件,同时调用的程序就会被执行。

(3)例子:

<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 内容选中事件 </title>
<script type="text/javascript">
  function message(){
    alert("您触发了选中事件!"); }
</script>    
</head>
<body>
  <form>
  个人简介:<br>
   <textarea name="summary" cols="60" rows="5" οnselect="message()">请写入个人简介,不少于200字!</textarea>
  </form>
</body>
</html>

 

7.文本框内容改变事件(onchange)

(1)语法:οnchange="message()";

(2)作用:通过改变文本框的内容来触发onchange事件,同时执行被调用的程序。

(3)例子:

<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 文本框内容改变事件 </title>
<script type="text/javascript">
  function message(){
    alert("您改变了文本内容!"); }
</script>    
</head>
<body>
  <form>
  个人简介:<br>
   <textarea name="summary" cols="60" rows="5" οnchange="message()">请写入个人简介,不少于200字!</textarea>
  </form>
</body>
</html>

 

8.加载事件(onload)

(1)语法:οnlοad="message()";

(2)作用:事件会在页面加载完成后,立即发生,同时执行被调用的程序

(3)例子:

<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 加载事件 </title>
<script type="text/javascript">
  function message(){
    alert("加载中,请稍等…"); }
</script>    
</head>
<body οnlοad="message()">
  欢迎学习JavaScript。
</body>
</html>

(4)注意:1. 加载页面时,触发onload事件,事件写在<body>标签内。2. 加载页面,可理解为打开一个新页面时。

 

9.卸载事件(onunload)

(1)语法:window.onunload = message;

(2)作用:当用户退出页面时(页面关闭、页面刷新等),触发onunload事件,同时执行被调用的程序。

(3)例子:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 卸载事件 </title>
<script type="text/javascript">   
     window.onunload = message;   
     function message(){   
        alert("您确定离开该网页吗?");   
    }   
</script>   
</head>
<body>
  欢迎学习JavaScript。
</body>
</html>

 

转载于:https://www.cnblogs.com/LeeYom1993/p/4339902.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值