addEventListener on NodeList

前端 未结 8 2081
轮回少年
轮回少年 2020-11-30 07:12

Does NodeList support addEventListener. If not what is the best way to add EventListener to all the nodes of the NodeList. Currently I am using the code snippet as show be

相关标签:
8条回答
  • 2020-11-30 08:07

    Another solution is to use event-delegation. You just add an eventlistener to the closets parent of the ".coins" and use event.target in the callback to check if the click was really on an element with the class "coins".

    0 讨论(0)
  • 2020-11-30 08:09

    The best I could come up with was this:

    const $coins = document.querySelectorAll('.coins')
    $coins.forEach($coin => $coin.addEventListener('dragstart', handleDragStart));
    

    Note that this uses ES6 features, so please make sure to transpile it first!

    0 讨论(0)
提交回复
热议问题