未捕获的引用错误:未在loadEventListeners上定义表单

时间:2018-08-02 22:28:20

标签: javascript

我不明白为什么会出现此错误。这使我的loadEventListeners没有定义。我不明白为什么它没有定义...任何输入都会有所帮助。

//Define ui var
const from = document.querySelector('#post-form');
const postList = document.querySelector('.collection');
const clearBtn = document.querySelector('.clear-posts');
const filter = document.querySelector('#filter');
const postInput = document.querySelector('#post');

// load all event listeners
loadEventListeners();

//load all event listners
function loadEventListeners(){
  //add Post Event
  form.addEventListener('submit', addPost);
}

//Add post
function addPost(e){
  if(postInput.value === ''){
    alert('Add a post');
}

  //create li element
  const li = document.createElement('li');
  //add class
  li.className = 'collection-item';
  //create text node and append to li
  li.appendChild(document.createTextNode(postInput.value));
  //create new link element
  const link = document.createElement('a');
  //add class
  link.className = 'delete-item secondary-content';
  //add icon html
  link.innerHTML= '<i class="fa fa-remove"></i>';
  //append the link to li
  li.appendChild(link);

  //append li to ul
  console.log(li);

  e.preventDefault();
}

1 个答案:

答案 0 :(得分:2)

第一行有一个错字……应该是form,而不是from

const form = document.querySelector('#post-form');