[Javascript] HTML5 DOM project
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | import './assets/css/style.css' ; const app = document.getElementById( 'app' ); app.innerHTML = ` <div class = "todos" > <div class = "todos-header" > <h3 class = "todos-title" >Todo List</h3> <div> <p>You have <span class = "todos-count" ></span> items</p> <button type= "button" class = "todos-clear" style= "display: none;" > Clear Completed </button> </div> </div> <form class = "todos-form" name= "todos" > <input type= "text" placeholder= "What's next?" name= "todo" > </form> <ul class = "todos-list" ></ul> </div> `; // state let todos = JSON.parse(localStorage.getItem('todos ')) || []; // selectors const root = document.querySelector(' .todos '); const list = root.querySelector(' .todos-list '); const count = root.querySelector(' .todos-count '); const clear = root.querySelector(' .todos-clear '); const form = document.forms.todos; const input = form.elements.todo; // functions function saveToStorage(todos) { localStorage.setItem(' todos ', JSON.stringify(todos)); } function renderTodos(todos) { let todoString = ' '; todos.forEach((todo, index) => { todoString += ` <li data-id="${index}"${todo.complete ? ' class = "todos-complete" ' : ' '}> <input type="checkbox"${todo.complete ? ' checked ' : ' '}> <span>${todo.label}</span> <button type="button"></button> </li> `; }); list.innerHTML = todoString; count.innerText = todos.filter(todo => !todo.complete).length; clear.style.display = todos.filter(todo => todo.complete).length ? ' block ' : ' none '; } function addTodo(event) { event.preventDefault(); const label = input.value.trim(); const complete = false; todos = [ ...todos, { label, complete, }, ]; renderTodos(todos); saveToStorage(todos); input.value = ' '; } function updateTodo(event) { // find span' s parnet node 's data-id attribute const id = parseInt(event.target.parentNode.getAttribute(' data-id '), 10); const complete = event.target.checked; todos = todos.map((todo, index) => { if (index === id) { return { ...todo, complete, }; } return todo; }); renderTodos(todos); saveToStorage(todos); } function editTodo(event) { // get current node tag name if (event.target.nodeName.toLowerCase() !== ' span ') { return; } const id = parseInt(event.target.parentNode.getAttribute(' data-id '), 10); const todoLabel = todos[id].label; const input = document.createElement(' input '); input.type = ' text '; input.value = todoLabel; function handleEdit(event) { // because list also has ' change ' event, so stop propgation event.stopPropagation(); // this point to input const label = this.value; if (label !== todoLabel) { todos = todos.map((todo, index) => { if (index === id) { return { ...todo, label, }; } return todo; }); renderTodos(todos); saveToStorage(todos); } // clean up event.target.style.display = ' '; // this point to input element ref this.removeEventListener(' change ', handleEdit); // remove input element this.remove(); } event.target.style.display = ' none '; event.target.parentNode.append(input); input.addEventListener(' change ', handleEdit); // focus has to be called in the last of function. // otherwise focus() has no effect input.focus(); } function deleteTodo(event) { if (event.target.nodeName.toLowerCase() !== ' button ') { return; } const id = parseInt(event.target.parentNode.getAttribute(' data-id '), 10); // get button' s sibling element span const label = event.target.previousElementSibling.innerText; if (window.confirm(`Delete ${label}?`)) { todos = todos.filter((todo, index) => index !== id); renderTodos(todos); saveToStorage(todos); } } function clearCompleteTodos() { const count = todos.filter(todo => todo.complete).length; if (count === 0) { return ; } if (window.confirm(`Delete ${count} todos?`)) { todos = todos.filter(todo => !todo.complete); renderTodos(todos); saveToStorage(todos); } } // init function init() { renderTodos(todos); // Add Todo form.addEventListener( 'submit' , addTodo); // Update Todo list.addEventListener( 'change' , updateTodo); // Edit Todo list.addEventListener( 'dblclick' , editTodo); // Delete Todo list.addEventListener( 'click' , deleteTodo); // Complete All Todos clear.addEventListener( 'click' , clearCompleteTodos); } init(); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2017-06-28 [WASM] Compile C Code into WebAssembly
2016-06-28 [Webpack 2] Use Karma for Unit Testing with Webpack
2016-06-28 [Webpack 2] Expose modules to dependencies with Webpack