Javascript nodeList.style.display=&x27;flex';它不起作用了
我找不到问题。我刚刚收到“UncaughtTypeError:Cannotsetproperty'display'of undefined”这个错误Javascript nodeList.style.display=&x27;flex';它不起作用了,javascript,flexbox,nodelist,Javascript,Flexbox,Nodelist,我找不到问题。我刚刚收到“UncaughtTypeError:Cannotsetproperty'display'of undefined”这个错误 function filterTodo(e){ const todos = todoList.childNodes; todos.forEach(function(todo){ switch (e.target.value){ case "all" : todo.style.dis
function filterTodo(e){
const 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';
}
break;
}
});
}
childNodes
包括所有节点,包括文本节点(没有样式
属性)
改为使用。它提供子元素。您可以在此处使用属性cahing
todo?.style?.display = 'flex'
它所做的是首先检查属性。如果它找到了确切的属性,然后赋值,否则将跳过。当我使用todoList.children时,则表明todos.forEach不是函数:/Uncaught syntaxer错误:赋值中的左侧无效,现在显示此错误,则可以使用此--->如果(todo.style.display)todo.style.display='flex';我认为todo不是我们期望的dom元素。您可以使用console.log()检查todo吗?
node.children
而不是node.childrends