一起学习网 一起学习网


构建简单Todo应用程序

开发 Todo app, JavaScript, HTML, CSS, web development 03-13

构建一个简单的Todo应用程序

在这篇文章中,我们将一起构建一个简单的Todo应用程序。这个应用程序将允许用户添加、删除和查看任务列表。我们将使用HTML、CSS和JavaScript来完成这一目标。

第一步:设置项目结构

首先,我们需要创建项目文件夹和必要的文件:

  • 一个HTML文件 (index.html)
  • 一个CSS文件 (styles.css)
  • 一个JavaScript文件 (app.js)

项目结构如下:

/TodoApp
  ├── index.html
  ├── styles.css
  └── app.js

第二步:创建HTML结构

index.html中,添加基本的HTML结构,包括输入框、添加按钮和任务列表展示区域。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>Simple Todo App</title>
</head>
<body>
    <div class="todo-container">
        <h1>Todo List</h1>
        <input type="text" id="todo-input" placeholder="Add a new task...">
        <button id="add-todo-btn">Add</button>
        <ul id="todo-list"></ul>
    </div>
    <script src="app.js"></script>
</body>
</html>

第三步:添加CSS样式

styles.css中,添加一些基本样式以使应用程序看起来更漂亮。

body {
    font-family: Arial, sans-serif;
    background-color: #f7f7f7;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
}

.todo-container {
    background-color: #fff;
    padding: 20px;
    border-radius: 5px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    width: 300px;
}

h1 {
    font-size: 24px;
    margin-bottom: 20px;
}

input[type="text"] {
    width: calc(100% - 22px);
    padding: 10px;
    margin-bottom: 10px;
    border: 1px solid #ddd;
    border-radius: 4px;
}

button {
    width: 100%;
    padding: 10px;
    border: none;
    background-color: #28a745;
    color: white;
    border-radius: 4px;
    cursor: pointer;
}

button:hover {
    background-color: #218838;
}

ul {
    list-style-type: none;
    padding: 0;
}

li {
    background-color: #f8f9fa;
    margin-bottom: 5px;
    padding: 10px;
    border-radius: 4px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

li button {
    background-color: #dc3545;
    border: none;
    color: white;
    border-radius: 4px;
    cursor: pointer;
    padding: 5px 10px;
}

第四步:实现JavaScript功能

app.js中,编写JavaScript代码来实现添加、删除任务的功能。

document.getElementById('add-todo-btn').addEventListener('click', function() {
    const todoInput = document.getElementById('todo-input');
    const todoText = todoInput.value.trim();
    
    if (todoText !== '') {
        const todoList = document.getElementById('todo-list');

        const todoItem = document.createElement('li');
        todoItem.textContent = todoText;

        const deleteBtn = document.createElement('button');
        deleteBtn.textContent = 'Delete';
        deleteBtn.addEventListener('click', function() {
            todoList.removeChild(todoItem);
        });

        todoItem.appendChild(deleteBtn);
        todoList.appendChild(todoItem);
        
        todoInput.value = '';
    }
});

总结

在这篇文章中,我们创建了一个简单的Todo应用程序,通过HTML构建了基本结构,使用CSS添加了样式,并通过JavaScript添加了交互功能,允许用户添加和删除任务。在这个过程中,我们学会了如何将不同的技术结合起来,构建一个简单但实用的Web应用程序。


编辑:一起学习网