1. 什么是事件
通常鼠标或热键的动作我们称之为 事件(Event),而由鼠标或热键引发的一连串程序的动作,称之为 事件驱动。对事件进行处理程序或函数,我们称之为 事件处理程序。

2.常用事件

1.onclick 单击
2.ondblclick 双击
3.onfocus 元素获得焦点
4.onblur 元素失去焦点
5.onmouseover 鼠标移到某元素之上
6.onmouseout 鼠标从某元素移开
7.onmousedown 鼠标按钮被按下
8.onmouseup 鼠标按键被松开
9.onkeydown 某个键盘按键被按下
10.onkeyup 某个键盘按键被松开
11.onkeypress 某个键盘按键被按下并松开

 

3.示例:

  <body>
  <div class="box" onmouseover="good()" onmouseout="out()">
  </div>
  <div class="down_up" onmousedown="down(this)" onmouseup="up(this)">
  </div>

  <input type="text" name="sdsad" value="焦点事件" onfocus="fouces(this)" onblur="b(this)">
  <script type="text/javascript">
  var inp = document.getElementsByTagName("input")[0];
  var box = document.getElementsByClassName("box")[0];
  function good(){ // 鼠标移入
  box.innerHTML = "good";
  }
  function out(){ // 鼠标移出
  box.innerHTML = "you have moved out";
  }
  function down(obj){ //鼠标按下
  obj.style.backgroundColor="green";
  obj.innerHTML = "green";
  }
  function up(obj){ // 鼠标抬起
  obj.style.backgroundColor="#999";
  obj.innerHTML = "glup";
  }
  function fouces(obj){ // 有焦点
  obj.value = "input cannot be empey";
  }
  function b(obj){ // 失去焦点
  inp.value = "";
  }
</script>
</body>

 

 

相关文章:

  • 2021-08-11
  • 2022-03-02
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-16
  • 2022-12-23
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
相关资源
相似解决方案