错误:无法设置未定义/a /at NodeList.forEach)/at HTMLSelectElement.filterTodo 的属性“display”
2020-05-24
643
错误 :
1. script.js:153 Uncaught TypeError: Cannot set property 'display' of
undefined
at script.js:153
at NodeList.forEach (<anonymous>)
at HTMLSelectElement.filterTodo (script.js:150)
代码 :
enter code here function filterTodo(e) {
let todos = todoList.childNodes;
todos.forEach(function(todo) {
switch (e.target.value) {
case "all":
todo.style.display = "flex";
break;
case "completed":
if (todo.classList.contains("completed")) {
todo.style.display = "flex";
} else {
todo.style.display = "none";
}
break;
case "uncompleted":
if (!todo.classList.contains("completed")) {
todo.style.display = "flex";
} else {
todo.style.display = "none";
}
}
});
}
3个回答
当您选择
let todos = todoList.childNodes;
时,请确保
todoList
元素之间没有任何空格。
例如:
var todoList = document.querySelector(".todo-list");
正确:
<ul class="todo-list"></ul>
错误:
<ul class="todo-list>
</ul>
manzt
2020-10-25
您可能应该检查
todos
包含什么。
您尝试分配
undefined.style.display = 'something'
23632t
2020-05-24
你可以在 todos 里面放非元素,
childNodes includes all child nodes—including non-element nodes like text and comment nodes. To get a collection of only elements, use ParentNode.children instead. From https://developer.mozilla.org/en-US/docs/Web/API/Node/childNodes
windmaomao
2020-05-24