一、绑定事件函数
绑定事件的说法是不正确的,准确来说是绑定事件处理函数。
事件会默认层层往上层dom节点传递。
let body = document.getElementsByTagName('body')[0];body.onclick = function(e){//w3c标准中e是传在事件处理函数中的,ie浏览器中,放在window.event中e = e || window.event//e为事件触发时,包含的相关信息与方法//MouseEvent为构造函数,mouseEvent为实例。console.log(e)//mouseEvent => MouseEvent.prototype => UIEvent.prototype => Event.prototype => Object.prototype//Event.prototype 里面有 stopPropagation方法,可以阻止冒泡。//mouseEvent里面有一个cancelBubble属性//根据w3c标准,可以调用e.stopPropogation来阻止冒泡,ie浏览器中,可以设置cancelBubble为true来实现if(e.stopProporgation){e.stopProporgation()}else{e.cancelBubble = true}}
阻止冒泡的方法:
(1)调用Event.prototype的stopPropogation方法 e.stopProporgation()
(2)将事件对象的cancelBubble设置为true e.cancelBubble = true
