需要實現的功能:將用戶輸入添加至待辦項,可以對todolist進行分類,用戶勾選即將待辦項分入已完成組,todolist的每一項可刪除和編輯,將用戶輸入數據寫入localStorage本地緩存,實現對輸入數據的保存,可以清楚域名下本地緩存,并清空所有todolist項。
具體功能的實現
HTML代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>todolist-prime</title> <link rel="stylesheet" href="yuansheng.css" rel="external nofollow" > </head> <body> <header> <section> <label for="add_list">My todolist</label> <input type="text" id="add_list" name="add_list" placeholder="type here" required> </section> </header> <p class="content"> <h1>未完成<span id="todocount"></span></h1> <ol id="todolist"> </ol> <h1>已完成<span id="donecount"></span></h1> <ol id="donelist"> </ol> </p> <p id="clear"> <span style="white-space:pre;"> </span><button id="clearbutton"><h3>全部清除</h3></button> </p> <script src="todolist-prime.js"></script> </body> </html>
JS代碼及分析
創建一個數組對象來保存用戶輸入的數據,數組的每一項都是一個對象,對象的"todo"屬性保存著用戶輸入的數據,"done"屬性可理解為用戶輸入數據的標簽,主要用來對"todo"值進行分類。
每次用戶輸入完數據,都要更新緩存,并初始化輸入框。
將輸入的數據添加至dom節點,并且根據輸入數據屬性("done")的值進行分類。
擊事項觸發編輯事件,將可編輯表單控件插入段落中,并將用戶輸入的值通過update函數對todolist數組里存儲的數據進行更新
將數組todolist相應項的屬性(“todo”或“done”)進行更新,并加載
刪除相應項,并加載
將用戶數據保存至本地緩存
從本地緩存中獲取數據,有數據,賦值給todolist,這樣刷新頁面用戶數據依舊存在
清楚本地緩存
一系列事件的監聽
CSS
body { margin: 0px; padding: 0px; font-size: 16px; background-color: gainsboro; } header { height: 50px; background-color: cornflowerblue; } header section { margin: 0 auto; width: 40%; } header section label { float: left; line-height: 50px; /*設置line-height和包含塊高度一致,以實現行內元素垂直居中*/ font-size: 20px; } #add_list { float: right; margin-top: 11px; width: 60%; height: 24px; border-radius: 5px; box-shadow: 0 1px 0 black; font-size: 18px; text-indent: 10px; } h1 { position: relative; } h1 span { position: absolute; top: 1px; right: 5px; display: inline-block; width: 23px; height: 23px; border-radius: 23px; /*創建圓形標記*/ line-height: 23px; font-size: 18px; text-align: center; background: #E6E6FA; } .content { width: 40%; margin: 0 auto; } li { position: relative; margin-bottom: 10px; border-radius: 5px; padding: 0 10px; height: 32px; box-shadow: 0 1px 0 black; line-height: 32px; background-color: burlywood; list-style: none; } ol li input { position: absolute; top: 4px; left: 10px; width: 20px; height: 20px; cursor: pointer; } p{ margin: 0; } ol li p { display: inline; margin-left: 35px; } ol li p input{ top: 5px; margin-left: 35px; width: 70%; height: 14px; font-size: 14px; line-height: 14px; } ol li a { position: absolute; top: 8px; right: 10px; display: inline-block; border: 1px; border-radius: 50%; width: 16px; height: 16px; font-size: 32px; line-height: 10px; color: red; font-weight: bolder; cursor: pointer; background-color: gray; } #clear { width: 100px; margin: 0 auto; } #clearbutton { border-color: red; border-radius: 5px; box-shadow: 0 1px 0 yellow; cursor: pointer; } button h3{ font-size: 13px; line-height: 13px; }
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com