gpt4 book ai didi

javascript - 未捕获的引用错误 : form is not defined at loadEventListeners

转载 作者:行者123 更新时间:2023-12-01 01:49:31 24 4
gpt4 key购买 nike

我不明白为什么会收到此错误。它告诉我我的 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();
}

最佳答案

第一行有一个拼写错误......它应该是form,而不是from

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

关于javascript - 未捕获的引用错误 : form is not defined at loadEventListeners,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51662958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com