使用JavaScript實(shí)現(xiàn)一個(gè)簡單的待辦事項(xiàng)列表todo-list
頁面效果
添加
刪除
分析
html分析
- 定義一個(gè)id為app,class為container的div容器;
- 在該容器內(nèi)再定義class為input-group的div容器,并在該容器內(nèi)定義一個(gè)class為label的div容器,class為content的input輸入框和class為btn的button;
- 在該容器內(nèi)再定義class為list的div容器,用于存放列表。
css涉及的小知識(shí)點(diǎn)
- 彈性容器默認(rèn)的主軸為x軸,子容器全部在主軸上對齊;
- 子容器默認(rèn)繼承彈性父容器100%的高;
- flex: 是否要放大 是否要縮小 設(shè)置的寬度;
- 居中:justify-content: center; align-items: center;
- .item:nth-child(1):class為item的第一個(gè)孩子容器。
js分析
要實(shí)現(xiàn)添加,刪除和渲染清單的功能
思路
var todoData = []; var addTodo = document.querySelector('.btn');//按鈕 var todoList = document.getElementById('todo-list');//獲取ul var close = document.querySelector('.close');//關(guān)閉按鈕
添加:先獲取添加按鈕的容器,然后給它綁定一個(gè)監(jiān)聽事件,當(dāng)點(diǎn)擊的時(shí)候觸發(fā)回調(diào)函數(shù),給定一個(gè)數(shù)組,用于存放todoList,獲取輸入框的內(nèi)容,加入數(shù)組,通過渲染數(shù)組將todoList顯示出來;
function addNewTodo(){ //獲取input的內(nèi)容 if (document.getElementById('newTodo').value.trim() != '') { todoData.push({ id:Math.floor(Date.now()),//時(shí)間戳 title:document.getElementById('newTodo').value, completed:true }); //渲染新的li render(); } };
刪除:先獲列表和關(guān)閉按鈕容器,然后給列表容器綁定一個(gè)監(jiān)聽事件,當(dāng)點(diǎn)擊的時(shí)候觸發(fā)回調(diào)函數(shù),先判斷是否點(diǎn)擊刪除按鈕,如果點(diǎn)擊了就獲取關(guān)閉按鈕的兄弟元素的內(nèi)容,再查找該內(nèi)容在數(shù)組中的下標(biāo),然后通過下標(biāo)在數(shù)組中進(jìn)行刪除,再渲染數(shù)組進(jìn)行更新;
function deleteTodo(event) { if (event.target.classList.contains('close')){ //首先獲取關(guān)閉按鈕元素 var close = event.target; //獲取close兄弟元素的內(nèi)容 var content = close.previousElementSibling.textContent; //查找要?jiǎng)h除元素的下標(biāo) var index = todoData.findIndex(item => item.title === content); //刪除該指定元素 todoData.splice(index, 1); //渲染li render(); } }
渲染:先定義一個(gè)空字符串,通過數(shù)組自帶的遍歷方法進(jìn)行遍歷,將該數(shù)組的元素通過列表顯示,將其賦值給str,最后將str設(shè)為todoList容器的內(nèi)容。
//將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">待辦事項(xiàng)</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');//關(guān)閉按鈕 //新增按鈕 function addNewTodo(){ //獲取input的內(nèi)容 if (document.getElementById('newTodo').value.trim() != '') { todoData.push({ id:Math.floor(Date.now()),//時(shí)間戳 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')){ //首先獲取關(guān)閉按鈕元素 var close = event.target; //獲取close兄弟元素的內(nèi)容 var content = close.previousElementSibling.textContent; //查找要?jiǎng)h除元素的下標(biāo) var index = todoData.findIndex(item => item.title === content); //刪除該指定元素 todoData.splice(index, 1); //渲染li render(); } } addTodo.addEventListener('click', addNewTodo); todoList.addEventListener('click',deleteTodo)
到此這篇關(guān)于使用JavaScript實(shí)現(xiàn)一個(gè)簡單的待辦事項(xiàng)列表todo-list的文章就介紹到這了,更多相關(guān)JavaScript待辦事項(xiàng)列表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js自動(dòng)生成的元素與頁面原有元素發(fā)生堆疊的解決方法
js自動(dòng)生成的元素與頁面原有元素發(fā)生堆疊通過去除浮動(dòng),給原有元素(商品擴(kuò)展信息部分)加上clear:both; 果然正常了2013-10-10微信小程序?qū)崿F(xiàn)默認(rèn)第一個(gè)選中變色效果
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)默認(rèn)第一個(gè)選中變色效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07基于Web?Components實(shí)現(xiàn)一個(gè)日歷原生組件
這篇文章主要為大家詳細(xì)介紹了如何利用Web?Components實(shí)現(xiàn)一個(gè)簡單的日歷原生組件,文中的示例代碼講解詳細(xì),需要的小伙伴可以了解一下2023-07-07小程序中監(jiān)聽頁面滾動(dòng)的幾種方法實(shí)例
這段時(shí)間接了一個(gè)微信小程序項(xiàng)目,從此打開小程序的新世界大門,下面這篇文章主要給大家介紹了關(guān)于小程序中監(jiān)聽頁面滾動(dòng)的幾種方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04js 多種變量定義(對象直接量,數(shù)組直接量和函數(shù)直接量)
js 多種變量定義(對象直接量,數(shù)組直接量和函數(shù)直接量),大家可以參考下,對于以后學(xué)習(xí)js 面向?qū)τ谂cjson操作會(huì)有幫助。2010-05-05Bootstrap每天必學(xué)之導(dǎo)航條(二)
Bootstrap每天必學(xué)之導(dǎo)航條,進(jìn)一步向大家講解了導(dǎo)航條養(yǎng)殖,以及導(dǎo)航條中元素的使用方法,感興趣的小伙伴們可以參考一下2016-03-03利用JS判斷字符串是否含有數(shù)字與特殊字符的方法小結(jié)
在我們?nèi)粘9ぷ鞯臅r(shí)候,利用javaScript判斷一個(gè)字符串中是否包括有數(shù)字和"-",在一些表單提交的地方,這是比較有用的常規(guī)判斷,這里收集有幾種不同的方法,最后還將簡要介紹下isNAN函數(shù)的使用方法和例子,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-11-11