记录一个 input change 和 blur 事件同时注册的问题

来源:互联网 发布:笔记本 外接显卡 知乎 编辑:程序博客网 时间:2024/06/05 06:12
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>测试 input 的 change 事件和 blur 事件</title></head><body>  <input id="input"></input>  <script type="text/javascript">      var input = document.getElementById("input");      var blurEvent = function(e) {console.log(1111111);      };      var changeEvent = function(e) {console.log(2222222);          var ent = e || window.event;//IE8及之前的event只能通过window.event来获得(针对对象属性注册方式)          if (ent.preventDefault) {console.log(ent.preventDefault);             ent.preventDefault(); //非标准技术          }          if (ent.returnValue) {console.log(ent.returnValue);             ent.returnValue = false; //IE          }          if (ent.stopPropagation) {console.log(ent.stopPropagation);             ent.stopPropagation();          }          if (ent.stopImmediatePropagation) {console.log(ent.stopImmediatePropagation);             ent.stopImmediatePropagation();          }          if (ent.cancelBubble) {console.log(ent.cancelBubble);             e.cancelBubble = true;          }          return false;      };      input.addEventListener("blur", blurEvent, false);      input.addEventListener("change", changeEvent, false);  </script></body></html>

结论:无论怎样都不能阻止先调用 change 后调用 blur......除非在 change 函数中使用 alert().......留待以后验证......

0 0