addEventListener on NodeList

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

    You could also use prototyping

    NodeList.prototype.addEventListener = function (type, callback) {
        this.forEach(function (node) {
            node.addEventListener(type, callback);
        });
    };
    

提交回复
热议问题