<div id="btns">
<button data-id="1">按钮1</button>
<button data-id="2">按钮2</button>
<button data-id="3">按钮3</button>
<button data-id="4">按钮4</button>
<div>666</div>
</div>
function bindEvent(obj, type, fn) {
if (obj.addEventListener) {
obj.addEventListener(type, eventFn);
} else {
obj.attachEvent("on" + type, eventFn);
}
function eventFn(e) {
var e = e || window.event;
var target = e.target || e.srcElement;
fn && fn.call(target,e)
}
}
bindEvent(document.getElementById('btns'),'click',function(e){console.log(e.target.innerHTML,e.target.dataset.id)})
手写事件代理(兼容IE浏览器)
最新推荐文章于 2024-06-16 22:10:34 发布
关键词由CSDN通过智能技术生成