Build a Simple JavaScript To-Do List
Building a Simple To-Do List Application with JavaScript
In this tutorial, we will create a simple to-do list application using HTML, CSS, and JavaScript. This application will allow users to add, remove, and mark tasks as completed. Let's dive into the steps to build this application.
Step 1: Set Up the HTML Structure
First, we need to create the basic HTML structure for our to-do list. We'll include an input field for adding new tasks, a button to submit them, 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 clean and organized.
/* styles.css */
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: 8px;
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: 4px;
}
button {
width: 100%;
padding: 10px;
background-color: #28a745;
color: #fff;
border: none;
border-radius: 4px;
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: #888;
}
Step 3: Implement the JavaScript Functionality
Now, let's add the JavaScript code to handle adding, removing, and marking tasks as completed.
// script.js
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 deleteBtn = document.createElement('button');
deleteBtn.textContent = 'Delete';
deleteBtn.addEventListener('click', () => {
taskList.removeChild(li);
});
li.appendChild(deleteBtn);
li.addEventListener('click', () => {
li.classList.toggle('completed');
});
taskList.appendChild(li);
taskInput.value = '';
}
});
Explanation
- HTML: We set up a simple structure with an input field, a button, and an unordered list to display tasks.
- CSS: Basic styling is applied to make the application visually appealing.
- JavaScript: We add event listeners to handle adding tasks, deleting tasks, and toggling the completion status of tasks.
With these steps, you have a fully functional to-do list application. You can further enhance it by adding features like editing tasks, saving tasks to local storage, or filtering tasks based on their completion status.
编辑:一起学习网