简体   繁体   English

Javascript EventListener 在创建另一个时消失

[英]Javascript EventListener disappears when another is created

I am trying to create a script where a select HTML object detects a change, gets its value and creates another tag to display based on its value.我正在尝试创建一个脚本,其中选择的 HTML 对象检测更改、获取其值并创建另一个标记以根据其值显示。 Eg A user selects 'United Kingdom' and a tag saying that appears in the div.例如,用户选择“英国”和出现在 div 中的标签。 This works, but I have an X-button for it that deletes the tag when clicked, using an EventListener .这有效,但我有一个 X 按钮,它在单击时使用EventListener删除标记。 The problem is that I can only ever delete the final tag created, as the variables used to create each tag's id change, and so, only the last one works.问题是我只能删除创建的最后一个标签,因为用于创建每个标签的 id 的变量会发生变化,因此,只有最后一个有效。 Some of the code is below:部分代码如下:

 document.getElementById("locations-editor-container1").innerHTML = 
    document.getElementById("locations-editor-container1").innerHTML+"<div 
    class='locations-editor-country-disp' id='"+country+"[@]'>"+country+"<svg  
   id='"+country+"[##]' class='locations-editor-x-button' 
   style='width:2vh;position:absolute;right:0.5vw;vertical-align:middle;' 
   viewBox='0 0 24 24'><path fill='#FFFFFF' 


      d='M19,6.41L17.59,5L12,10.59L6.41,
      5L5,6.41L10.59, 12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z'/></svg></div><br>";
            var countryId=country+"[##]";
            
     document.getElementById(countryId).addEventListener("click",function(){ remove(countryId)},false);

Below is your error reproduced, because you are replacing innerHTML of some content you are removing the event listeners of the elements you are replacing.下面是您重现的错误,因为您正在替换某些内容的 innerHTML,您正在删除要替换的元素的事件侦听器。 That is why only the last element works:这就是为什么只有最后一个元素有效:

 const content = document.querySelector("#content"); var counter = 0; const add = e => { content.innerHTML = //you are replacing content, thus removing the event listeners of // previous elements content.innerHTML + `<div style="cursor: pointer;" id="div${++counter}"> click me ${counter} </div>`; document.querySelector(`#div${counter}`) .addEventListener( "click" ,(x=>e=>{ alert(`Counter for this div:${x}`); })(counter) ); } document.querySelector("#myButton").addEventListener( "click" ,add );
 <div id="content"> </div> <input type="button" id="myButton" value="add">

The solution is to not replace the content but add the new item to existing content:解决方案是不替换内容,而是将新项目添加到现有内容中:

 const content = document.querySelector("#content"); var counter = 0; const add = e => { //create a div (will not be added to dom yet) const div = document.createElement("div"); div.innerHTML = `<div style="cursor: pointer;" id="div${++counter}"> click me ${counter} </div>`; div .addEventListener( "click" ,(x=>e=>{ alert(`Counter for this div:${x}`); })(counter) ); //add element to dom (do not replace content) content.appendChild(div); } document.querySelector("#myButton").addEventListener( "click" ,add );
 <div id="content"> </div> <input type="button" id="myButton" value="add">

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

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