index.js:16未捕获的参考错误:__________在HTMLDocument上未定义。<匿名>

时间:2019-12-17 10:06:04

标签: javascript

更改此代码后,代码将不再起作用

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事件侦听器内部定义这些变量。

0 个答案:

没有答案