删除待办事项

时间:2018-06-15 03:41:41

标签: javascript

我无法弄清楚如何让这段代码运行起来。我试图根据点击的项目删除待办事项。我的目标最终是根据点击的按钮进行删除。

到目前为止,这是我的代码:

import uuidv4 from 'uuid/v4'
let text;
let todos = [];

document.querySelector('#new-todo').addEventListener('keypress', e =>
{
    text = e.target.value;

    if (e.keyCode === 13 || e.which === 13) {
        e.preventDefault();
        addTodo(text);
        renderTodo();
        console.log(todos);
    }
});

const addTodo = text =>
{
    todos.push(
    {
        id: uuidv4(),
        text
    });
}

const renderTodo = () =>
{
    let node = document.createElement("p");
    let textnode = document.createTextNode(text);
    node.appendChild(textnode);
    document.getElementById("todos").appendChild(node);
}

document.querySelector('#todos').addEventListener('click', () =>
{
    removeTodo(todo.id);
    console.log(todos);
});

const removeTodo = id => 
{
    const todoIndex = todos.findIndex((todo) => todo.id === id)

    if (todoIndex > -1) 
    {
        todos.splice(todoIndex, 1);
    }
};

0 个答案:

没有答案