Javascript Event Listener on click not working as expected

前端 未结 5 421
感动是毒
感动是毒 2021-01-20 15:54

I\'m trying to create one event listener to handle all my \"clicks\" by putting it on the body and doing some event delegation. A simple example of what i\'m trying to do i

5条回答
  •  执笔经年
    2021-01-20 16:49

    You should use e.target.parentNode.parentNode.id =='div1' See the demo:

    document.body.addEventListener('click', function(e) {
      if (e.target.parentNode.parentNode.id == "div1") {
        alert("hi")
      }
    })
    • 1
    • 2
    • 3

提交回复
热议问题