- 自己觸發的事件,讓他的父元素代替偵聽
<!-- 節點 -->
<body>
<div id="rect"></div>
</body>
/**
* 在父節點偵聽子節點事件
*/
document.body.addEventListener('click',function(e)
{
console.log(e.target.id) //輸出:rect
})
<!-- 節點 -->
<body>
<div id="rect"></div>
</body>
/**
* 在父節點偵聽子節點事件
*/
document.body.addEventListener('click',function(e)
{
console.log(e.target.id) //輸出:rect
})