Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

javascript - Uncaught TypeError: todo.classList is undefined

I don't understand why this error is shown. I define 3 to 4 functions but all are working properly but when I write this function its through me a error. What could be its cause?

Uncaught TypeError: todo.classList is undefined

function filterTodo(e){
    const todos = todoList.childNodes;
    console.log(todos);
    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";
                }
            case "uncompleted":
                if(!todo.classList.contains("completed")){
                    todo.style.display = "flex";
                } else {
                    todo.style.display = "none";
                }
         }
    });
}
question from:https://stackoverflow.com/questions/65642888/uncaught-typeerror-todo-classlist-is-undefined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

classList is a member of Element. todo is the iterator of todos, which was initialized as

const todos = todoList.childNodes;

childNodes is a NodeList, which has Node items inside of it. Node is an interface, one of its implementations is Element. So, the troubling item is a Node, but not an Element. I would find out what it is if I were you. A simple fix would be

todo.classList && todo.classList.contains("completed")

at both occasions, but I would dig deeper to find out what the issue is if I were you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...