更改此代码后,代码将不再起作用
document.addEventListener("DOMContentLoaded", () => {
// your code here
document.getElementById('create-task-form').addEventListener("submit", function(event) {
let list = document.getElementById('tasks');
let element = document.createElement('li');
let item = document.getElementById("new-task-description");
let tasks = document.querySelectorAll('div#list ul li button');
element.innerHTML = `${item.value} <button data-description="${item.value}">x</button>`;
list.appendChild(element);
item.value = "";
event.preventDefault();
});
tasks.addEventListener('click', function(event) {
const comment = event.target.parentElement
comment.remove();
});
});
到
document.addEventListener("DOMContentLoaded", () => {
// your code here
document.getElementById('create-task-form').addEventListener("submit", function(event) {
let list = document.getElementById('tasks');
let element = document.createElement('li');
let item = document.getElementById("new-task-description");
let anythingElse = document.querySelectorAll('div#list ul li button');
element.innerHTML = `${item.value} <button data-description="${item.value}">x</button>`;
list.appendChild(element);
item.value = "";
event.preventDefault();
});
anythingElse.addEventListener('click', function(event) {
const comment = event.target.parentElement
comment.remove();
});
});
请注意,我收到错误Uncaught ReferenceError: anythingElse is not defined
at HTMLDocument.<anonymous>
,而以前在该位置使用单词tasks
时没有错误。 任务已更改为其他。
这里到底是什么问题?这是完整详细信息https://github.com/dlyons8/jsdom-task-lister-lite-v-000的仓库。
我的目标是更改click
事件侦听器的名称。我已经尝试在submit
事件侦听器之外和DOMContent
事件侦听器内部定义这些变量。