一起学习网 一起学习网


JavaScript To-Do List Application Tutorial

开发 JavaScript tutorial, to-do list app, beginner web development, HTML CSS JavaScript project, DOM manipulation 03-09

Creating a Simple To-Do List Application with JavaScript

In this tutorial, we will build a simple to-do list application using HTML, CSS, and JavaScript. This application will allow users to add, remove, and mark tasks as completed. It's a great project for beginners to practice their web development skills.

Step 1: Set Up the HTML Structure

First, we need to create the basic HTML structure for our to-do list application. We'll have an input field for adding new tasks, a button to submit the task, and a list to display the tasks.

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

Step 2: Style the Application with CSS

Next, let's add some basic styling to make our application look better. Create a styles.css file and add the following styles:

body {
    font-family: Arial, sans-serif;
    background-color: #f4f4f9;
    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 0 10px rgba(0, 0, 0, 0.1);
    width: 300px;
}

h1 {
    text-align: center;
}

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

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

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

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

li {
    padding: 10px;
    border-bottom: 1px solid #ddd;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

li.completed {
    text-decoration: line-through;
    color: #999;
}

Step 3: Implement the JavaScript Functionality

Now, let's add the JavaScript to handle adding, removing, and marking tasks as completed. Create a script.js file and add the following code:

document.addEventListener('DOMContentLoaded', () => {
    const taskInput = document.getElementById('task-input');
    const addTaskBtn = document.getElementById('add-task-btn');
    const taskList = document.getElementById('task-list');

    addTaskBtn.addEventListener('click', addTask);

    function addTask() {
        const taskText = taskInput.value.trim();
        if (taskText === '') return;

        const li = document.createElement('li');
        li.textContent = taskText;

        const removeBtn = document.createElement('button');
        removeBtn.textContent = 'Remove';
        removeBtn.addEventListener('click', () => {
            taskList.removeChild(li);
        });

        li.appendChild(removeBtn);
        li.addEventListener('click', () => {
            li.classList.toggle('completed');
        });

        taskList.appendChild(li);
        taskInput.value = '';
    }
});

Explanation

  • HTML: We created a simple structure with an input field, a button, and an unordered list to display tasks.
  • CSS: Basic styling was added to make the application visually appealing.
  • JavaScript: We added event listeners to handle adding tasks, removing tasks, and toggling the completed state of tasks.

This simple to-do list application is a great starting point for learning how to manipulate the DOM with JavaScript and can be expanded with additional features like task editing, persistence using local storage, and more.


编辑:一起学习网