简体   繁体   English

Javascript:我为每个 class 中的所有按钮添加了一个事件侦听器,但某些按钮不起作用

[英]Javascript: I added an event listener to all button in every class but some button won't work

I just started studying JS and I'm currently simulating something that will apply to my project Basically, I'm trying to generate new Divs with Button on it in order to do something.我刚开始学习 JS,我目前正在模拟一些适用于我的项目的东西基本上,我正在尝试生成带有 Button 的新 Div 以做某事。 And I applied the for loop on the button from the guide here Turns out it works?我在此处指南中的按钮上应用了 for 循环原来它有效? but there's a bug where some buttons wont work whenever I generate more divs+button and I don't know why?但是有一个错误,当我生成更多 div+按钮时,某些按钮将无法工作,我不知道为什么?

 const btnCreate = document.querySelector(".myButton"); const changeBtn = document.getElementsByClassName("changeBtnStyle"); const newNewDiv = document.getElementsByClassName("newCDiv"); const createFunc = function() { const parentDiv = document.querySelector(".parentDiv"); const newDiv = document.createElement("div"); const newChangeBtn = document.createElement("button"); parentDiv.appendChild(newDiv); newDiv.appendChild(newChangeBtn); newDiv.classList.add("newCDiv"); newChangeBtn.classList.add("changeBtnStyle") newChangeBtn.innerHTML = "change" for (let i = 0; i < changeBtn.length; i++) { changeBtn[i].addEventListener("click", function() { newNewDiv[i].classList.toggle("changeColor"); }, false); } }; btnCreate.addEventListener("click", createFunc);
 .parentDiv { margin: auto; width: 300px; height: 300px; background-color: gray; position: relative; }.newCDiv { background: green; width: 50px; height: 50px; position: relative; }.changeBtnStyle { position: absolute; }.changeColor { background: red; }.myButton { margin: auto; }
 <button class="myButton"> Create Div w/ Button </button> <div class="parentDiv"> </div>

Here's the JSFiddle one这是JSFiddle一个

Every time you click on a generated button the for loop will add an event listener for each button.每次单击生成的按钮时, for循环都会为每个按钮添加一个事件侦听器。 Even the ones that already have an event listener attached to them.即使是那些已经附加了事件侦听器的。 So by doing that and then toggling the class, you call the classList.toggle() function more than once.因此,通过这样做然后切换 class,您不止一次调用classList.toggle() function。

For example with 1 event listener, the toggle works fine.例如,对于 1 个事件侦听器,切换工作正常。 But with 2 event listeners you toggle and toggle again, resulting in an immediate on / off switch.但是使用 2 个事件侦听器,您可以再次切换和切换,从而立即打开/关闭开关。 3 event listeners will toggle 3 times, on / off / on, having the right resulting but not working correctly. 3 个事件侦听器将切换 3 次,开/关/开,得到正确的结果但无法正常工作。

So instead of looping each button again, just add the event listener only to the element that you've created in the createFunc function.因此,无需再次循环每个按钮,只需将事件侦听器仅添加到您在createFunc function 中创建的元素。

 const btnCreate = document.querySelector(".myButton"); const changeBtn = document.getElementsByClassName("changeBtnStyle"); const newNewDiv = document.getElementsByClassName("newCDiv"); const createFunc = function() { const parentDiv = document.querySelector(".parentDiv"); const newDiv = document.createElement("div"); const newChangeBtn = document.createElement("button"); parentDiv.appendChild(newDiv); newDiv.appendChild(newChangeBtn); newDiv.classList.add("newCDiv"); newChangeBtn.classList.add("changeBtnStyle") newChangeBtn.innerHTML = "change" newChangeBtn.addEventListener("click", function() { newDiv.classList.toggle("changeColor"); }, false); }; btnCreate.addEventListener("click", createFunc);
 .parentDiv { margin: auto; width: 300px; height: 300px; background-color: gray; position: relative; }.newCDiv { background: green; width: 50px; height: 50px; position: relative; }.changeBtnStyle { position: absolute; }.changeColor { background: red; }.myButton { margin: auto; }
 <button class="myButton">Create Div w/ Button</button> <div class="parentDiv"></div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM