简体   繁体   English

为什么事件监听器只添加到第一个元素?

[英]Why is the event listener added only to the first element?

So, after a button is clicked, there is this loop which goes through the array and creates a div containing data from each array element.因此,单击按钮后,会出现这个循环,它会遍历数组并创建一个包含每个数组元素数据的 div。 Now, after the divs are injected into DOM, I add an event listener to them, but only the firstly added element responds.现在,在将 div 注入 DOM 之后,我向它们添加了一个事件侦听器,但只有第一个添加的元素会响应。 :/ The others are dead. :/ 其他人都死了。 Any tips?有小费吗?

 btnLeft.addEventListener("click", function(){ let output = ""; for(let x of array){ output += `<div class="listItem" data-id="${x.id}"> <ul> <li>Name: ${x.name}</li> <li>Price: ${x.price}</li> </ul> </div>`; } leftList.innerHTML = output; document.querySelector(".listItem").addEventListener("click", function(){ console.log(this.getAttribute("data-id")); }); });

The Document method querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. Document 方法 querySelector() 返回文档中与指定选择器或选择器组匹配的第一个元素。 If no matches are found, null is returned.如果未找到匹配项,则返回 null。

Since, document.querySelector(".listItem") is only selecting the first element that matches the specified selector, the event listener is applied only to the first element.由于document.querySelector(".listItem")仅选择与指定选择器匹配的第一个元素,因此事件侦听器仅应用于第一个元素。

The Document method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors. Document 方法 querySelectorAll() 返回一个 static(非实时)NodeList,表示与指定选择器组匹配的文档元素列表。

Try using document.querySelectorAll(".listItem") and loop through this to add event listeners to all the elements.尝试使用document.querySelectorAll(".listItem")并循环遍历它以将事件侦听器添加到所有元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未将事件侦听器添加到div元素 - Event Listener not getting added to the div element 将事件侦听器添加到新添加的DOM元素中 - Adding event listener to a newly added DOM element 向元素添加了一个滚动事件监听器,但它仅在 scrollTop !== 0 时才以正确的方式移动 - Added a scroll event listener to an element, but it moves the right way only when scrollTop !== 0 为什么此事件侦听器会在添加之前接收已发送的事件 - Why does this event listener pick up an event sent before it was added 事件侦听器仅适用于最后创建的元素 - Event listener only applying to last created element TodoList 网页,比 mouseovert/out 更好的事件监听器? 为什么我的伪元素 ::first-letter 不起作用? - TodoList Webpage, better event listener than mouseovert/out? why is my pseudo element ::first-letter not working? 为什么 mouseenter 事件侦听器仅适用于第一个网格项,但不适用于 rest - Why is the mouseenter event listener only working on the first grid-item but is not working on the rest 多个事件监听器被添加到每个元素导致多个ajax调用 - Multiple event listener are added to each element resulting in multiple ajax call 为正文中添加的每个新元素添加事件侦听器 - Adding event listener for each new element added on body 动态添加子元素的事件监听器也标记为Parent - Dynamically added child element's event listener tagged to Parent also
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM