事件笔记

270 阅读1分钟
  1. onload事件会在所有资源加载完毕后执行,而 DOMContentLoaded 是在html加载并解析完成后才会触发。意思是onload在 DOMContentLoaded 之后触发且 DOMContentLoaded 之能通过 addEventListener 方法监听。两个事件都只能在window上监听。
window.onload = function (e) {
  console.log('window onload');
};
      
window.addEventListener('DOMContentLoaded', function (e) {
  console.log('DOMContentLoaded');
});
  1. onscroll可以在window, body, document.documentElement上监听。body子元素只能在该元素上进行监听。

  2. mouseout,mouseover在子元素上也会触发。mouseenter,mouseleave不会在子元素上触发。

  3. 键盘事件上顺序为 keydown, keypress, onbeforeinput, input, keyup。

    1. keypress只会在输入字符是触发,如:h,a...;按下ctrl不会触发。
    2. onbeforeinput firefox不会触发。
    3. 安卓低版本chrome内核onbeforeinput不能阻止默认行为。
    4. ios contenteditable 删除最后一个字符后再次按删除键时不会触发onbeforeinput。