JavaScript 待办事项列表划掉所有其他元素

标签 javascript html css web-applications

我建立了这个待办事项列表网站: https://mickanie.github.io/toDoList/ 但我注意到,当我将自己的元素添加到默认元素时,它只允许我划掉列表中的所有其他元素。

image更准确地说,我无法在所附图片上划掉 item1、item3 和 item5。有谁知道这是为什么以及如何解决它?

用于从列表中划掉元素的 JavaScript 函数:

const setDoneOnOff = () => {
li = document.querySelectorAll("li");
for (let i=0; i<li.length; i++) {
    li[i].addEventListener("click", function(f) {
        f.currentTarget.classList.toggle("done");
    });
}

我什至不知道问题是否出在那里(网站源代码中的完整代码)。

最佳答案

我不明白为什么人们需要用 Javascript 做一些简单的事情。

说明:也可以用div包起来,不一定要是form。

<form>
 <input type="checkbox" id="abc"><label for="abc">abc</label> 
 <input type="checkbox" id="123"><label for="123">123</label>
 <input type="checkbox" id="omg"><label for="omg">omg</label>
</form>

<style>
form{
  display:block;
}

input[type="checkbox"]:checked + label{
/* Target only input with a type of checkbox.
 * If the type of checkbox is checked, cross the input and label.
 * Keep in mind that you can NOT cross a checkbox.
 * But it must be there as a "event handler".
 * The plus is a selector that means something like "and" label
 * The input is the checkbox that toggles "checked" and the label is the actual text that gets crossed. */
  text-decoration: line-through;
}
</style>

jsfiddle

如果您不想要复选框,请将 input[type="checkbox"]{ display:none } 添加到您的样式中。

jsfiddle

关于JavaScript 待办事项列表划掉所有其他元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54100655/

相关文章:

javascript - 让js对象循环变得多余

javascript - 自动/电影滚动文本框效果 CSS/JS

html - IMG 和背景图像均不显示 SVG 图像

css - 如何制作响应式图片库?

jquery - 在移动 View bootstrap3 上制作 Accordion

javascript - Excel Office-js : Merge 2 or more cells

javascript - 使用 Node js、AngularJs 和 JWT 使用用户名和密码进行身份验证

html - 使用显示器 :inline-block not aligning 垂直对齐图像

html - 将文本限制为仅两行

javascript - 在没有请求访问 token 的情况下检查用户是否喜欢 Facebook 页面