nodeList.style.display = 'flex' 无效

nodeList.style.display = 'flex' is not working

我找不到问题。我刚收到“Uncaught TypeError: Cannot set 属性 '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 包括 所有 节点,包括文本节点(没有 style 属性.

改用children。这提供了 child 元素 .

您可以在此处使用 属性 cahining....

todo?.style?.display = 'flex'

它所做的是首先检查 属性。如果它找到确切的 属性 则分配该值,否则跳过。