1.事件冒泡:
通俗易懂的来讲,就是当一个子元素的事件被触发的时候(如onclick事件),该事件会从事件源(被点击的子元素)开始逐级向上传播,触发父级元素的点击事件。下面见详细的代码:
<div id="parent" style="background-color: #000;height: 400px;width: 400px" data-id="444">
<div id="child" style="background-color: #fff;height: 200px;width: 200px" data-id="555"></div>
</div>
document.getElementById('parent').οnclick=function () {
console.log(this.getAttribute('data-id'));
};
document.getElementById('child').οnclick=function () {
console.log(this.getAttribute('data-id'));
};