addEventListener on NodeList

前端 未结 8 2281
轮回少年
轮回少年 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:01

    There actually is a way to do this without a loop:

    [].forEach.call(nodeList,function(e){e.addEventListener('click',callback,false)})
    

    And this way is used in one of my one-liner helper libraries - nanoQuery.

提交回复
热议问题