[英]How do I make the checkboxes work separately for each list item?
I've appended checkboxes to each appended list item. 我已将复选框附加到每个附加的列表项。 I'm trying to get each checkbox to work distinctly for each list item, but when I click the checkbox, all of the list items get struck through.
我试图让每个复选框对每个列表项都可以单独工作,但是当我单击复选框时,所有列表项都会被删除。 I am thinking the solution requires creating a for loop to create distinct id's for each appended list item, but I'm having trouble accomplishing this.
我在想该解决方案需要创建一个for循环来为每个附加的列表项创建不同的ID,但是我很难做到这一点。 Any guidance would be greatly appreciated.
任何指导将不胜感激。 Thank you.
谢谢。
var inputtedChoreVar= document.getElementById("inputtedChore");
var toDoListVar= document.getElementById("toDoList");
var toDoArray= [];
document.getElementById('subButton').addEventListener('click',hitSubmit);
function hitSubmit() {
let x= inputtedChoreVar.value;
toDoArray.push(x);
console.log(toDoArray);
localStorage.setItem('toDoKey', toDoArray);
var newListElement= document.createElement('LI');
var newTextNode= document.createTextNode(x);
newListElement.appendChild(newTextNode);
toDoListVar.appendChild(newListElement);
var fButtonElement= document.createElement('input');
fButtonElement.setAttribute('type','checkBox');
newListElement.appendChild(fButtonElement);
fButtonElement.onclick= strike;
function strike() {
document.getElementById('toDoList').style.textDecoration='line-through';
}
}
you should apply the style to the li element and not the whole list. 您应该将样式应用于li元素,而不是整个列表。
function strike() {
newListElement.style.textDecoration='line-through';
}
Your strike()
function is setting a style on the entire toDoList
element. 您的
toDoList
strike()
函数正在整个toDoList
元素上设置样式。 You probably want to scope that to your LI
. 您可能希望将其范围限制到您的
LI
。
var inputtedChoreVar= document.getElementById("inputtedChore");
var toDoListVar= document.getElementById("toDoList");
var toDoArray= [];
document.getElementById('subButton').addEventListener('click',hitSubmit);
function hitSubmit() {
let x= inputtedChoreVar.value;
toDoArray.push(x);
console.log(toDoArray);
localStorage.setItem('toDoKey', toDoArray);
var newListElement= document.createElement('LI');
var newTextNode= document.createTextNode(x);
newListElement.appendChild(newTextNode);
toDoListVar.appendChild(newListElement);
var fButtonElement= document.createElement('input');
fButtonElement.setAttribute('type','checkBox');
newListElement.appendChild(fButtonElement);
fButtonElement.onclick= function() { strike(newListElement); } // function that calls "strike" with an argument
}
function strike(el) {
el.style.textDecoration='line-through';
}
I took the time to rewrite your code in the following way: 我花了一些时间通过以下方式重写您的代码:
Your first issue was that you was assigning the text-decoration
property to the toDoListElement
and each children was inheriting it. 您的第一个问题是,您正在将
text-decoration
属性分配给toDoListElement
而每个子代都继承该属性。
You don't need to loop or to assign multiple different ids because JavaScript will save variable references for you (if you use the local variable newListElement
inside your event handler, when the handler is executed it will use the last value that the variable had). 您不需要循环或分配多个不同的ID,因为JavaScript会为您保存变量引用(如果您在事件处理程序中使用局部变量
newListElement
,则在执行处理程序时,它将使用该变量具有的最后一个值) 。
let inputElement = document.getElementById("inputElement"); let toDoListElement = document.getElementById("toDoList"); document.getElementById('subButton').addEventListener('click', hitSubmit); function hitSubmit() { let newListElement = document.createElement('li'); let checkboxElement = document.createElement('input'); checkboxElement.setAttribute('type','checkbox'); checkboxElement.addEventListener('change', function strike(e) { if (e.target.checked) { newListElement.style.textDecoration = 'line-through'; } else { newListElement.style.removeProperty('text-decoration') } }); checkboxElement.style.marginLeft = '10px'; newListElement.innerText = inputElement.value; newListElement.appendChild(checkboxElement); toDoListElement.appendChild(newListElement); }
<input id="inputElement"></div> <button id="subButton" type="button">ADD</button> <div id="toDoList"></div>
Finally, for the localStorage
part, I didn't include it in the snippet, but I would use the following: 最后,对于
localStorage
部分,我没有在代码段中包含它,但是我将使用以下内容:
localStorage.setItem('toDoKey', JSON.stringify((JSON.parse(localStorage.getItem('toDoKey')) || []).concat([inputElement.value])));
You was passing the array directly to localStorage.setItem()
which only accepts string values (for that you use JSON.stringify()
and JSON.parse()
) 您将数组直接传递到仅接受字符串值的
localStorage.setItem()
(为此您使用JSON.stringify()
和JSON.parse()
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.