Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nodeList.style.display=&x27;flex';它不起作用了_Javascript_Flexbox_Nodelist - Fatal编程技术网

Javascript nodeList.style.display=&x27;flex';它不起作用了

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

我找不到问题。我刚刚收到“UncaughtTypeError:Cannotsetproperty'display'of undefined”这个错误

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吗?
  • something
  • 这是console.log(todo)的输出,只需使用
    node.children
    而不是
    node.childrends