Create a Simple To-Do List App
Creating 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 get started!
Step 1: Set Up the HTML Structure
First, we'll create the basic HTML structure for our to-do list application. This will include an input field for adding new tasks, a button to submit the tasks, and a list to display them.
<!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, we'll add some basic styles to make our application look better. Create a styles.css
file and add the following 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: 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 {
background-color: #f8f9fa;
margin-bottom: 5px;
padding: 10px;
border-radius: 3px;
display: flex;
justify-content: space-between;
align-items: center;
}
li.completed {
text-decoration: line-through;
color: #6c757d;
}
Step 3: Implement the JavaScript Functionality
Now, let's add the JavaScript code 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', () => {
const taskText = taskInput.value.trim();
if (taskText !== '') {
addTask(taskText);
taskInput.value = '';
}
});
taskList.addEventListener('click', (e) => {
if (e.target.tagName === 'LI') {
e.target.classList.toggle('completed');
} else if (e.target.tagName === 'BUTTON') {
e.target.parentElement.remove();
}
});
function addTask(taskText) {
const li = document.createElement('li');
li.textContent = taskText;
const removeBtn = document.createElement('button');
removeBtn.textContent = 'Remove';
li.appendChild(removeBtn);
taskList.appendChild(li);
}
});
Explanation
- HTML: We set up a basic structure with an input field, a button, and an unordered list to display tasks.
- CSS: We styled the application to make it visually appealing, using flexbox for centering and basic styling for elements.
- JavaScript: We added event listeners to handle adding tasks, toggling completion, and removing tasks. The
addTask
function creates a new list item with a remove button for each task.
With these steps, you have a fully functional to-do list application. You can expand this application by adding features like task editing, due dates, or local storage to save tasks between sessions.
编辑:一起学习网