使用JavaScript實現(xiàn)一個簡單的待辦事項列表todo-list
頁面效果
添加
刪除
分析
html分析
- 定義一個id為app,class為container的div容器;
- 在該容器內再定義class為input-group的div容器,并在該容器內定義一個class為label的div容器,class為content的input輸入框和class為btn的button;
- 在該容器內再定義class為list的div容器,用于存放列表。
css涉及的小知識點
- 彈性容器默認的主軸為x軸,子容器全部在主軸上對齊;
- 子容器默認繼承彈性父容器100%的高;
- flex: 是否要放大 是否要縮小 設置的寬度;
- 居中:justify-content: center; align-items: center;
- .item:nth-child(1):class為item的第一個孩子容器。
js分析
要實現(xiàn)添加,刪除和渲染清單的功能
思路
var todoData = []; var addTodo = document.querySelector('.btn');//按鈕 var todoList = document.getElementById('todo-list');//獲取ul var close = document.querySelector('.close');//關閉按鈕
添加:先獲取添加按鈕的容器,然后給它綁定一個監(jiān)聽事件,當點擊的時候觸發(fā)回調函數(shù),給定一個數(shù)組,用于存放todoList,獲取輸入框的內容,加入數(shù)組,通過渲染數(shù)組將todoList顯示出來;
function addNewTodo(){ //獲取input的內容 if (document.getElementById('newTodo').value.trim() != '') { todoData.push({ id:Math.floor(Date.now()),//時間戳 title:document.getElementById('newTodo').value, completed:true }); //渲染新的li render(); } };
刪除:先獲列表和關閉按鈕容器,然后給列表容器綁定一個監(jiān)聽事件,當點擊的時候觸發(fā)回調函數(shù),先判斷是否點擊刪除按鈕,如果點擊了就獲取關閉按鈕的兄弟元素的內容,再查找該內容在數(shù)組中的下標,然后通過下標在數(shù)組中進行刪除,再渲染數(shù)組進行更新;
function deleteTodo(event) { if (event.target.classList.contains('close')){ //首先獲取關閉按鈕元素 var close = event.target; //獲取close兄弟元素的內容 var content = close.previousElementSibling.textContent; //查找要刪除元素的下標 var index = todoData.findIndex(item => item.title === content); //刪除該指定元素 todoData.splice(index, 1); //渲染li render(); } }
渲染:先定義一個空字符串,通過數(shù)組自帶的遍歷方法進行遍歷,將該數(shù)組的元素通過列表顯示,將其賦值給str,最后將str設為todoList容器的內容。
//將todoData中的數(shù)據(jù)渲染出來 function render(){ var str = ''; todoData.forEach(function(item){ str += ` <li class="item"> <div class="flex"> <input type="checkbox" class="item-check"> <p class="item-content">${item.title}</p> <span class="close">x</span><!-- uusx --> </div> </li> `; }); todoList.innerHTML = str; };
addTodo.addEventListener('click', addNewTodo); todoList.addEventListener('click',deleteTodo)
完整代碼
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>todoList</title> <link rel="stylesheet" href="./style.css" rel="external nofollow" > </head> <body> <div id="app" class="container"> <h2 class="title">Todo List</h2> <div class="input-group"> <div class="label">待辦事項</div> <input type="text" class="content" id="newTodo"> <button class="btn">新增</button> </div> <div class="list"> <!-- ul>li*2{$}--> <ul id="todo-list"> </ul> </div> </div> <script src="./index.js"></script> </body> </html>
css
/* html{ height: 100%; } */ *{ padding: 0; margin: 0; } li{ list-style: none; } body{ display: flex; justify-content: center; align-items: center; /* height: 100%; */ height: 100vh; } .container{ width: 400px; height: 400px; background: linear-gradient(#da4453,#89216b); } .title{ text-align: center; margin: 10px; } .input-group{ display: flex; } .label{ /* 上右下左 */ padding: 5px 10px; } .btn{ padding: 5px 10px; margin-left: 10px; } .content{ flex: 1; } .item:nth-child(1){ margin-top: 20px; } .item{ border-bottom: 1px solid #eee; } .flex{ display: flex; width: 90%; /* m0-a */ margin: 0 auto; align-items: center; } .item-check{ margin-right: 20px; } .item-content{ flex: 13; } .close{ width: 30px; height: 30px; border: 1px solid #aaa; font-size: 20px; text-align: center; border-radius: 10px; cursor: pointer; }
js
var todoData = []; var addTodo = document.querySelector('.btn');//按鈕 var todoList = document.getElementById('todo-list');//獲取ul var close = document.querySelector('.close');//關閉按鈕 //新增按鈕 function addNewTodo(){ //獲取input的內容 if (document.getElementById('newTodo').value.trim() != '') { todoData.push({ id:Math.floor(Date.now()),//時間戳 title:document.getElementById('newTodo').value, completed:true }); //渲染新的li render(); } }; //將todoData中的數(shù)據(jù)渲染出來 function render(){ var str = ''; todoData.forEach(function(item){ str += ` <li class="item"> <div class="flex"> <input type="checkbox" class="item-check"> <p class="item-content">${item.title}</p> <span class="close">x</span><!-- uusx --> </div> </li> `; }); todoList.innerHTML = str; }; function deleteTodo(event) { if (event.target.classList.contains('close')){ //首先獲取關閉按鈕元素 var close = event.target; //獲取close兄弟元素的內容 var content = close.previousElementSibling.textContent; //查找要刪除元素的下標 var index = todoData.findIndex(item => item.title === content); //刪除該指定元素 todoData.splice(index, 1); //渲染li render(); } } addTodo.addEventListener('click', addNewTodo); todoList.addEventListener('click',deleteTodo)
到此這篇關于使用JavaScript實現(xiàn)一個簡單的待辦事項列表todo-list的文章就介紹到這了,更多相關JavaScript待辦事項列表內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于Web?Components實現(xiàn)一個日歷原生組件
這篇文章主要為大家詳細介紹了如何利用Web?Components實現(xiàn)一個簡單的日歷原生組件,文中的示例代碼講解詳細,需要的小伙伴可以了解一下2023-07-07js 多種變量定義(對象直接量,數(shù)組直接量和函數(shù)直接量)
js 多種變量定義(對象直接量,數(shù)組直接量和函數(shù)直接量),大家可以參考下,對于以后學習js 面向對于與json操作會有幫助。2010-05-05利用JS判斷字符串是否含有數(shù)字與特殊字符的方法小結
在我們日常工作的時候,利用javaScript判斷一個字符串中是否包括有數(shù)字和"-",在一些表單提交的地方,這是比較有用的常規(guī)判斷,這里收集有幾種不同的方法,最后還將簡要介紹下isNAN函數(shù)的使用方法和例子,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-11-11