简单的Todo List实现(附源码)
效果图(这里没办法显示动态,具体动态自己复制代码去网页看):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Todo List</title> <style> body { background-color: #f6f6f6; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 0; padding: 0; } .container { max-width: 600px; margin: 20px auto; background-color: #fff; border-radius: 10px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); padding: 20px; } .header { text-align: center; margin-bottom: 20px; color: #3f51b5; } .todo-form { display: flex; align-items: center; margin-bottom: 20px; } .todo-input { flex: 1; padding: 10px; border: none; border-radius: 5px; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); } .todo-button { padding: 10px 20px; border: none; border-radius: 5px; background-color: #3f51b5; color: #fff; cursor: pointer; } .todo-list { list-style: none; padding: 0; } .todo-item { display: flex; align-items: center; padding: 10px; border-radius: 5px; margin-bottom: 10px; background-color: #e8e8e8; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); } .todo-item:last-child { margin-bottom: 0; } .todo-item-checkbox { margin-right: 10px; } .todo-item-text { flex: 1; color: #3f51b5; font-weight: bold; } .todo-item-delete { padding: 5px 10px; border: none; border-radius: 5px; background-color: #ff6b6b; color: #fff; cursor: pointer; transition: background-color 0.3s; } .todo-item-delete:hover { background-color: #ff4f4f; } </style> </head> <body> <div class="container"> <h1 class="header">Todo List</h1> <form class="todo-form" id="todoForm" action=""> <input type="text" class="todo-input" placeholder="Add a new todo" id="todoInput"> <button type="submit" class="todo-button">Add</button> </form> <ul class="todo-list" id="todoList"> </ul> </div> </body> </html> <script> const todoForm = document.getElementById("todoForm") const todoList = document.getElementById("todoList") const todoInput = document.getElementById("todoInput") function createToDoItem(text) { const li = document.createElement('li') li.className = "todo-item" li.innerHTML = ` <input type="checkbox" class="todo-item-checkbox"> <span class="todo-item-text">${text}</span> <button class="todo-item-delete">Delete</button>` return li } todoForm.onsubmit = function (event) { event.preventDefault()//onsubmit事件应该跳转新页面,用这个消除跳转 /* console.log(todoInput.value.trim())//用户输入的内容 trim且去除空格*/ const todoText = todoInput.value if (!todoText) return //弱类型转换,转换为布尔值!'' 非空是true const li = createToDoItem(todoText.trim())//trim去掉字符串头尾里的空格 // console.log(li) todoList.appendChild(li)//把生成的li放到ul里面去 todoInput.value = '' } todoList.onclick = function (event) { // console.log(event.target,event.target.parentNode) if (event.target.className === "todo-item-delete" && confirm("Are you sure you want to delete this?")) { event.target.parentNode.remove()//confirm是弹出框 } } </script>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构