wraper
box
本文共 1689 字,大约阅读时间需要 5 分钟。
contentwraperbox
利用事件冒泡,和事件源对象(e[非IE事件对象].target或event[IE事件对象].srcElement)进行处理。
事件捕获只需要把监听事件的第三个参数,false改为true。
box.addEventListener('click',function(e){ console.log('box') },true) wraper.addEventListener('click',function(e){ console.log('wraper') },true) content.addEventListener('click',function(e){ console.log('content') },true)
// 事件捕获 box.addEventListener('click',function(e){ console.log('box捕获') },true) wraper.addEventListener('click',function(e){ console.log('wraper捕获') },true) content.addEventListener('click',function(e){ console.log('content捕获') },true) // 事件冒泡 box.addEventListener('click',function(e){ console.log('box冒泡') },false) wraper.addEventListener('click',function(e){ console.log('wraper冒泡') },false) content.addEventListener('click',function(e){ console.log('content冒泡') },false)
使用e.stopPropagation()
IE使用event.cancelBubble = true(兼容所有IE) 或 event.stopPropagation() (IE9以下不兼容)
box.addEventListener('click',function(e){ e.stopPropagation() console.log('box冒泡') },false) wraper.addEventListener('click',function(e){ console.log('wraper冒泡') },false) content.addEventListener('click',function(e){ console.log('content冒泡') },false)
转载地址:http://nkhwz.baihongyu.com/