简体   繁体   English

似乎无法弄清楚如何正确删除 childNodes

[英]Cannot seem to work out how to remove childNodes properly

So I have build a little display.所以我做了一个小展示。 If you click on a filter word it is supposed to display a tag.如果你点击一个过滤词,它应该会显示一个标签。 Thats working so far.到目前为止工作正常。 Thats the first code snippet.那是第一个代码片段。

But If I click on the filter word I want it to be removed from the document again and I cant figure that out.但是,如果我单击过滤词,我希望它再次从文档中删除,但我无法弄清楚。 That would be the second function. I want to iterate thorugh the created "display" elements and remove them again if they contain the "clicked value" area.那将是第二个 function。我想遍历创建的“显示”元素,如果它们包含“点击值”区域,则再次删除它们。 The out commented stuff did not work I either get the error that leng is not a function or that he "cannot read property 0 of undefined at tagFunction".注释掉的东西不起作用我要么得到 leng 不是 function 的错误,要么他“无法读取 tagFunction 处未定义的属性 0”。

Thank you for your help !谢谢您的帮助 !

 var area = event.target.textContent; var spa = document.createElement("span"); var p = document.createElement("p"); spa.setAttribute('class', 'display'); p.setAttribute("class", "display1") var text = document.createTextNode(area); spa.appendChild(p); p.appendChild(text); p.addEventListener("click", tagFunction); document.getElementById("tags123").appendChild(spa);
 <div class="tags123" id="tags123"> </div>

 function tagFunction() { var area = event.target.textContent; var leng = document.getElementsByClassName("display1"); var tags123 =document.getElementsByClassName("tags123"); for(var ll = 0;ll<leng.length;ll++){ if(leng[ll].innerText.trim().contains(area)){ //tags123.removeChild(leng(ll)); tags123.children[ll].remove(); // leng(ll).remove(); } } }

This was the error.这就是错误。 I had to declare parentNode of the leng[ll] element first.我必须首先声明 leng[ll] 元素的 parentNode。

             if(leng[ll].innerText.trim().contains(bereich)){
                 var vader = leng[ll].parentNode;
                 document.getElementById("tags123").removeChild(vader);

There are two error in two lines:两行有两个错误:

if(leng[ll].innerText.trim().includes(area)){ //should be contains and not includes

leng(ll).remove(); // should be [ ] and not ( )

it will be like this:它将是这样的:

if(leng[ll].innerText.trim().contains(area)){

leng[ll].remove();

 var area = "text test"; var spa = document.createElement("span"); var p = document.createElement("p"); spa.setAttribute('class', 'display'); p.setAttribute("class", "display1") var text = document.createTextNode(area); spa.appendChild(p); p.appendChild(text); p.addEventListener("click", tagFunction); document.getElementById("tags123").appendChild(spa); function tagFunction() { var area = event.target.textContent; var leng = document.getElementsByClassName("display1"); var tags123 =document.getElementsByClassName("tags123"); console.log(leng); for(var ll = 0; ll <= leng.length; ll++){ if(leng[ll].innerText.trim().includes(area)){ leng[ll].remove(); } } }
 <div class="tags123" id="tags123"> </div>

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

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