DOM事件

1. 基本概念:DOM事件的级别

事件级别:DOM0   element.onclick=function(){}

DOM2    element.addEventListener('click',function(){},false)

DOM3    element.addEventListener('keyup',function(){},false)

2. DOM事件模型:捕获和冒泡

捕获是从上往下,冒泡是从当前元素往上

一个完整的事件流分三个阶段:捕获--目标阶段--冒泡

描述DOM事件捕获的具体流程

window--document--html--body--父级--子级--目标元素

3. Event对象的常见应用

event.preventDefault() 阻止默认行为

event.stopPropagation()  阻止冒泡

event.stopImmediatePropagation()  事件响应优先级

event.currentTarget 当前所绑定的事件

event.target

4. 自定义事件

var eve=new Event('custome');

ev.addEventListener('custom',function(){

  console.log('custom')

});

ev.dispatchEvent(eve);

具体例子见代码https://github.com/leitingting08/Front-end-language/blob/master/layout/event.html

原文地址:https://www.cnblogs.com/leiting/p/8261679.html