javascript - 未捕获的引用错误:表单未在 loadEventListeners 中定义
问题描述
我不明白为什么会收到此错误。它给了我我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');
推荐阅读
- sql - 如何知道包含特定键的 JSON 列表?
- android-studio - Android Studio中的Flutter编译错误
- angular - 使用一个调用 observable 的函数,其中 3 个不同的 observable 与 switch map 绑定在一起。导致错误
- python - 为什么我在使用 BeautifulSoup.find_all 函数时添加了 '/' 字符?
- binding - 使用 Azure 函数绑定时如何减少 CreateTable 事务?
- javascript - 如何从html文件中的linux路径加载json文件
- sql - 根据 SQL Server 中的特定条件对行进行分组
- java - Socket Python-Server and Java - android studio中的客户端,其中java无法连接到python服务器
- javascript - 如何使用 Slonik 将多条记录插入我的 postgres 数据库?
- java - 如何在链表中用 Java 编写包含方法?