big.addEventListener(‘click‘,function(){ console.log("大"); },true) //在函数后面加true可以使这个div在捕捉阶段就被捕捉输出 middle.addEventListener(‘click‘,function(){ console.log("中"); }) small.addEventListener(‘click‘,function(){ console.log("小"); },true)
box.attachEvent(‘onclick‘,function(){ console.log("点击了div"); })
function bindEvent(ele,type,handler){ if(ele.addEventListener){ ele.addEventListener(type,handler) }else if(ele.attachEvent){ ele.attachEvent(‘on‘+type,handler) }else{ ele[‘on‘ + type] = handler } } function fn(){ console.log("点击了div"); } bindEvent(box,‘click‘,fn) function fun(){ console.log("进来了"); } bindEvent(box,‘mouseover‘,fun)
原文:https://www.cnblogs.com/mrxiachongyu/p/14264392.html