Assign event handlers to dynamically created buttons
问题 I am trying to assign a click event handler to dynamically created buttons that once clicked, will return the ID of the clicked button in vanilla Javascript without any frameworks. Yet I can't seem to get the events to handle properly, here's the code let h = document.getElementsByClassName("buttons"); h.forEach(function() { addEventListener("click", function() { alert(this.id); }); }; 回答1: Try let h = document.getElementsByClassName("buttons"); [...h].forEach(b => { b.addEventListener("click