简体   繁体   English

如何将事件侦听器添加到动态创建的 HTML 列表元素?

[英]How do I add event listeners to dynamically created HTML list elements?

I'm attempting to make a to-do list application where when a clicked, a list element toggles to a class checked that adds a strikethrough style.我正在尝试制作一个待办事项列表应用程序,当单击时,列表元素切换到添加删除线样式的checked类。 I'm having trouble targeting one list element at a time and not checking off the whole list.我在一次定位一个列表元素并且没有检查整个列表时遇到问题。 How can I dynamically add event listeners for each item?如何为每个项目动态添加事件侦听器? Currently, with the below code, items added are already checked and I cannot figure out why.目前,使用以下代码,已检查添加的项目,我无法弄清楚原因。

My HTML appears as follows:我的 HTML 显示如下:

<div id="body">
            <form id="form" onsubmit="newElement()" target="_self">
                <input type="text" id="task" placeholder="What's on the agenda?">
            </form>

            <ul id="list">
                <li class = "checked">test</li >
            </ul>
        </div>

My function grabs data from a text box through this function我的函数通过这个函数从文本框中抓取数据

function newElement() {
    event.preventDefault(); // stop default redirect

    /* create a list item and put the inputted text within */
    var li = document.createElement("li"); // create a list item
    var inputValue = document.getElementById("task").value; // value of text box
    var t = document.createTextNode(inputValue); // create a text node of the box value
    li.appendChild(t); // put the text node in the li
    li.addEventListener('click', checkToggle(li)); // add event listener for a click to toggle
                                                   // a strikethrough

    /* append the li item to the ul */
    appendItem("list", li); 
}

and then the checkToggle function appears as follows然后checkToggle函数出现如下

/* make items get checked when clicked */
function checkToggle(li) {
    li.classList.toggle("checked");
}

However, when the page is loaded, every element is automatically checked when added already.但是,当页面加载时,每个元素在添加时都会自动检查。

You are executing function - checkToggle(li)您正在执行功能 - checkToggle(li)

You should attach function like addEventListener('click', checkToggle) without parenthesis您应该附加类似addEventListener('click', checkToggle)类的函数addEventListener('click', checkToggle)不带括号

 function newElement() { event.preventDefault(); // stop default redirect /* create a list item and put the inputted text within */ var li = document.createElement("li"); // create a list item var inputValue = document.getElementById("task").value; // value of text box var t = document.createTextNode(inputValue); // create a text node of the box value li.appendChild(t); // put the text node in the li li.addEventListener('click', checkToggle); // add event listener for a click to toggle // a strikethrough /* append the li item to the ul */ document.querySelector("ul").appendChild(li); } function checkToggle() { this.classList.toggle("checked"); }
 .checked{color: blue;}
 <div id="body"> <form id="form" onsubmit="newElement()" target="_self"> <input type="text" id="task" placeholder="What's on the agenda?"> </form> <ul id="list"> <li class = "checked">test</li > </ul> </div>

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

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