简体   繁体   English

在嵌套循环 vanilla JS 中动态添加和删除元素

[英]adding and removing elements dynamically within nested loops vanilla JS

I Want to create a dynamic list of time ranges within list of date ranges and give each generated ul and li a specific class/id.我想在日期范围列表中创建一个动态的时间范围列表,并为每个生成的 ul 和 li 指定一个特定的类/ID。

This is how far I got.这是我走了多远。 It gives an error that newElement is null!它给出了 newElement 为空的错误! I saw some answers with generating rows, but you can't give them a specific class/ids.我看到了一些生成行的答案,但你不能给他们一个特定的类/ID。

I like it this way better since you have control over each ul and li.我更喜欢这种方式,因为您可以控制每个 ul 和 li。

 var list = document.getElementById('date'); var add = document.getElementById('add'); //adding a new element to the list addDate.addEventListener('click', function(){ var newElement = document.createElement('LI'); list.appendChild(newElement); var i; for (i=1;i<10; i++){ newElement.innerHTML = "NEW Date Frame<span class='btn'>X</span><ul id='timeframe-"+[i]+"'></ul><button id='addTime'>Add a Time Frame</button>"; var addTime= document.getElementById('addTime'); var timeframe = document.getElementById('timeframe'); addTime.addEventListener('click', function(){ var newElement = document.createElement('LI'); timeframe.appendChild(newElement); newElement.innerHTML= "NEW Time Frame<button class='btn'>X</button>"; }); } }); //removing list.addEventListener('click', function(e){ if(e.target && e.target.nodeName == "SPAN") { // List item found! Output the ID! e.target.parentNode.remove(); } });
 ul span { cursor: pointer; color: blue; margin: 5px; }
 <div> <ul id="date"> <li class="element">Date Frame</li> </ul> <button id="addDate">Add a Date Frame</button> </div>

You are creating multiple timeframes but you were selecting only one:您正在创建多个时间范围,但您只选择了一个:

var timeframe = document.getElementById('timeframe');

What you need to do is selecting specific timeframe each loop:您需要做的是选择每个循环的特定时间范围:

var timeframe = document.getElementById('timeframe-' +[i]);

 var list = document.getElementById('date'); var add = document.getElementById('add'); //adding a new element to the list addDate.addEventListener('click', function(){ var newElement = document.createElement('LI'); list.appendChild(newElement); var i; for (i=1;i<10; i++){ newElement.innerHTML= "NEW Date Frame<span class='btn'>X</span><ul id='timeframe-"+[i]+"'></ul><button id='addTime'>Add a Time Frame</button>"; var addTime= document.getElementById('addTime'); var timeframe = document.getElementById('timeframe-' +[i]); addTime.addEventListener('click', function(){ var newElement = document.createElement('LI'); timeframe.appendChild(newElement); newElement.innerHTML= "NEW Time Frame<button class='btn'>X</button>"; }); } }); //removing list.addEventListener('click', function(e){ if(e.target && e.target.nodeName == "SPAN") { // List item found! Output the ID! e.target.parentNode.remove(); } });
 ul span { cursor: pointer; cursor: pointer; color: blue; margin: 5px; }
 <div> <ul id="date"> <li class="element">Date Frame</li> </ul> <button id="addDate">Add a Date Frame</button> </div>

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

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