Add Event Listener to Collection of HTML Elements

前端 未结 8 2091
执念已碎
执念已碎 2020-12-03 18:40

I know that getElementsByTagName and getElementsByClassName need an index identifier in order for the objects to be bound to an event listener.

8条回答
  •  醉酒成梦
    2020-12-03 19:30

    Try querySelectorAll method.

    var inputElem = document.querySelectorAll('input');
    

    Which returns a NodeList and you can loop through the array to add the event listeners.

提交回复
热议问题