[js]js小demo,待办事项列表
1、初步实现功能,只有前端代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>待办事项列表</title>
<style>
body {
font-family: 'Arial', sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
h1 {
text-align: center;
color: #333;
}
.todo-container {
background-color: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.input-container {
display: flex;
margin-bottom: 20px;
}
#todo-input {
flex: 1;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px 0 0 4px;
font-size: 16px;
}
#add-button {
padding: 10px 15px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 0 4px 4px 0;
cursor: pointer;
font-size: 16px;
}
#add-button:hover {
background-color: #45a049;
}
ul {
list-style-type: none;
padding: 0;
}
li {
padding: 10px;
border-bottom: 1px solid #eee;
display: flex;
justify-content: space-between;
align-items: center;
}
li:last-child {
border-bottom: none;
}
.todo-text {
flex: 1;
}
.delete-button {
background-color: #f44336;
color: white;
border: none;
border-radius: 4px;
padding: 5px 10px;
cursor: pointer;
}
.delete-button:hover {
background-color: #d32f2f;
}
.completed {
text-decoration: line-through;
color: #888;
}
</style>
</head>
<body>
<h1>待办事项列表</h1>
<div class="todo-container">
<div class="input-container">
<input type="text" id="todo-input" placeholder="输入新的待办事项...">
<button id="add-button">添加</button>
</div>
<ul id="todo-list"></ul>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const todoInput = document.getElementById('todo-input');
const addButton = document.getElementById('add-button');
const todoList = document.getElementById('todo-list');
// 从本地存储加载待办事项
let todos = JSON.parse(localStorage.getItem('todos')) || [];
// 渲染待办事项列表
function renderTodos() {
todoList.innerHTML = '';
todos.forEach((todo, index) => {
const li = document.createElement('li');
const todoText = document.createElement('span');
todoText.textContent = todo.text;
todoText.className = todo.completed ? 'todo-text completed' : 'todo-text';
const deleteButton = document.createElement('button');
deleteButton.textContent = '删除';
deleteButton.className = 'delete-button';
deleteButton.addEventListener('click', () => deleteTodo(index));
if (todo.completed) {
const toggleButton = document.createElement('button');
toggleButton.textContent = '已完成';
toggleButton.style.backgroundColor = '#2196F3';
toggleButton.style.marginRight = '10px';
toggleButton.addEventListener('click', () => toggleTodo(index));
li.appendChild(toggleButton);
} else {
const toggleButton = document.createElement('button');
toggleButton.textContent = '完成';
toggleButton.style.backgroundColor = '#4CAF50';
toggleButton.style.marginRight = '10px';
toggleButton.addEventListener('click', () => toggleTodo(index));
li.appendChild(toggleButton);
}
li.appendChild(todoText);
li.appendChild(deleteButton);
todoList.appendChild(li);
});
// 保存到本地存储
localStorage.setItem('todos', JSON.stringify(todos));
}
// 添加新的待办事项
function addTodo() {
const text = todoInput.value.trim();
if (text) {
todos.push({
text: text,
completed: false
});
todoInput.value = '';
renderTodos();
}
}
// 删除待办事项
function deleteTodo(index) {
todos.splice(index, 1);
renderTodos();
}
// 切换待办事项的完成状态
function toggleTodo(index) {
todos[index].completed = !todos[index].completed;
renderTodos();
}
// 添加事件监听
addButton.addEventListener('click', addTodo);
todoInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
addTodo();
}
});
// 初始渲染
renderTodos();
});
</script>
</body>
</html>

浙公网安备 33010602011771号