在 forEach 方法中从 nodeList 更改为 Array 时出错
Error in Changing from nodeList to Array in forEach method
我已尽最大努力使用不同的方法将我的 nodeList 转换为 Array 但它仍然出现此错误
Uncaught TypeError: 无法设置未定义的 属性 'display'
在 Project.js:68
在 Array.forEach
()
在 HTMLSelectElement.filterTodo
我真的需要一个解决方案,或者更好的是,如果我做错了什么,我需要一个更正
function filterTodo(e) {
const todos = Array.from(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';
}
}
})
}
todoList.childNodes
还将包括非元素节点,如文本和评论。这些非元素节点没有 style
属性,当您尝试通过 todo.style.display
来操作这些节点时,它会正确地抛出此错误
Cannot set property 'display' of undefined
要获取仅包含元素节点的集合,请改用 todoList.children
。
(另外nodeList转Array也没有问题,没问题)
我已尽最大努力使用不同的方法将我的 nodeList 转换为 Array 但它仍然出现此错误
Uncaught TypeError: 无法设置未定义的 属性 'display'
在 Project.js:68
在 Array.forEach
()
在 HTMLSelectElement.filterTodo
我真的需要一个解决方案,或者更好的是,如果我做错了什么,我需要一个更正
function filterTodo(e) {
const todos = Array.from(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';
}
}
})
}
todoList.childNodes
还将包括非元素节点,如文本和评论。这些非元素节点没有 style
属性,当您尝试通过 todo.style.display
来操作这些节点时,它会正确地抛出此错误
Cannot set property 'display' of undefined
要获取仅包含元素节点的集合,请改用 todoList.children
。
(另外nodeList转Array也没有问题,没问题)