jQuery案例之ToDoList
1. 需求
使用jQuery配合html与css完成以下界面

2. 案例分析
2.1 说明
这里不纠结于html与css代码的搭建,着重处理其中的js需求
2.2 js事件原理
2.21 案例:案例介绍
2.22 案例:toDoList 分析
2.23 案例:toDoList 按下回车把新数据添加到本地存储里面
2.24 案例:toDoList 本地存储数据渲染加载到页面
2.25 案例:toDoList 删除操作
2.26 案例:toDoList 正在进行和已完成选项操作
2.27 案例:toDoList 统计正在进行个数和已经完成个数
3. 页面结构
3.1 html结构
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
| <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>todolist</title> <link rel="stylesheet" href="style.css"> <script src="../jquery-3.6.0.js"></script> <script src="todo.js"></script> </head>
<body> <header> <section> <label for="title">ToDolist</label> <input type="text" id="title" placeholder="添加todo" required="required" autocomplete="off"> </section> </header> <section> <h2>正在进行<span id="todocount">0</span></h2> <ol id="todolist" class="demo-class">
</ol> <h2>已经完成<span id="donecount">0</span></h2> <ul id="donelist">
</ul> </section> <footer> <p>(请按下<strong>空格键</strong>输入内容,然后按下<strong>enter</strong>回车)</p> copyright © 2014 todolist.cn </footer> <a href="javascript:;" class="up" title="back">↑</a> </body>
</html>
|
3.2 css结构
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
| * { padding: 0; margin: 0; }
body, html { background-color: rgb(204, 204, 204); scroll-behavior: smooth; }
header { background-color: #000; padding: 20px 0; width: 100%; position: fixed; top: 0;
section { margin: 0 auto; width: 50%;
label { color: white; line-height: 25px; font-size: 25px; font-weight: bolder; }
input { width: 50%; height: 25px; float: right; padding-left: 15px; border-radius: 15px; border: none; outline: none; box-shadow: inset 0px 2px 5px black; }
.big { animation: cartoon .5s linear alternate; }
@keyframes cartoon { from { transform: scale(1.3); }
to {} }
} }
section { margin: 0 auto; width: 50%; margin-top: 70px;
h2 { padding: 10px 0;
span { width: 20px; height: 20px; line-height: 20px; text-align: center; font-size: 15px; border-radius: 50%; background-color: aliceblue; float: right; } }
ol { list-style: none;
li { width: 100%; height: 40px; border-radius: 5px; line-height: 40px; margin: 10px 0; background-color: aliceblue; border-left: 3px solid green;
input { width: 20px; height: 20px; float: left; margin: 10px 15px 0; }
span { width: 20px; height: 20px; border: 2px solid #6d6d6d; text-align: center; line-height: 20px; border-radius: 50%; float: right; margin: 10px 10px 0; cursor: pointer; } } }
ul { list-style: none;
li { width: 100%; height: 40px; border-radius: 5px; line-height: 40px; margin: 10px 0; background-color: rgb(167, 169, 171); border-left: 5px solid #999; opacity: 0.5;
input { width: 20px; height: 20px; float: left; margin: 10px 15px 0; }
span { width: 20px; height: 20px; border: 2px solid #6d6d6d; text-align: center; line-height: 20px; border-radius: 50%; float: right; margin: 10px 10px 0; cursor: pointer; } } } }
footer { margin-top: 50px; line-height: 30px; text-align: center;
p { text-align: center; padding: 10px 0; font-size: 15px;
strong { font-size: 20px; } } }
a{ text-decoration: none; color: black; background-color: white; display: none; width: 30px; height: 30px; border-radius: 50%; border: 1px solid #000000; text-align: center; line-height: 30px; font-weight: bolder; position: fixed; right: 5%; bottom: 30%; }
|
3.3 js结构
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
|
$(function () { loadData()
$('#title').on('keydown', function (e) { if (e.keyCode === 13) { if ($(this).val() === '') { alert('内容不能为空') } else { var localData = getData()
localData.push({ title: $(this).val(), done: false })
saveData(localData)
loadData()
$(this).val('') }
} })
$('#todolist,#donelist').on('click', 'span', function () { var localData = getData() var index = $(this).attr('id') localData.splice(index, 1) saveData(localData) loadData() })
$('#todolist,#donelist').on('click', 'input', function () { var localData = getData() var index = $(this).siblings('span').attr('id') localData[index].done = $(this).prop('checked') saveData(localData) loadData() })
function getData() { var data = localStorage.getItem('todolist'); console.log(data); if (data !== null) { return JSON.parse(data); } else { return [] } }
function saveData(Data) { localStorage.setItem('todolist', JSON.stringify(Data)) console.log(Data); }
function loadData(e) { var loadData = getData();
if (loadData.length >= 15) { $('#title').attr({ 'disabled': true, 'placeholder': '每日仅限添加15条内容' }) } else { $('#title').attr({ 'disabled': false, 'placeholder': '添加todo' }) }
$('#todolist,#donelist').empty();
var todocount = 0 var donecount = 0 $.each(loadData, function (index, element) { if (element.done) { $('#donelist').prepend('<li>' + (index + 1) + '. ' + element.title + '<input type="checkbox" name="" id="" checked><span class="remove" id=' + index + '>-</span></li>') donecount++ } else { $('#todolist').prepend('<li>' + (index + 1) + '. ' + element.title + '<input type="checkbox" name="" id=""><span class="remove" id=' + index + '>-</span></li>') todocount++ } })
$('#todocount').html(todocount) $('#donecount').html(donecount) }
$(window).scroll(function () { if ($(document).scrollTop() >= 200) { $('.up').fadeIn() } else { $('.up').fadeOut() } $('.up').on('click', function () { $('body,html').scrollTop({ scrollTop: 0 }) }) }) $(document).on('keydown', function (e) { if (e.keyCode === 32) { $('#title').focus().addClass('big').val('') } })
})
|