- html部分
<!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>Document</title>
<link rel="stylesheet" href="./index.css">
</head>
<body>
<div class="box">
<h1>代办列表</h1>
<div class="box_none">
<input type="" name="" value="" placeholder="请输入代办事项">
</div>
<div class="lis">
</div>
<div class="complete">
<span class="left">1.未完成</span>
<span class="right"><span>清理</span> <i>0</i>已完成</span>
</div>
</div>
<script src="./index.js"></script>
</body>
</html>
2.css部分
*{
margin: 0;
padding: 0;
}
body{
background-color: rgba(110, 113, 114, 0.63);
}
input{
border: none;
outline: none;
color: #333;
}
li{
list-style: none;
}
.box{
margin:100px auto;
width: 800px;
background-color: #fff;
border-radius: 10px;
}
.box h1 {
margin: 10px 10px;
}
.box_none {
display: flex;
justify-content: center;
}
.box_none input{
padding-left: 20px;
margin: 20px;
width:750px;
height: 40px;
border-radius: 10px;
border: 1px solid #333;
}
.lis {
margin-left: 20px;
}
.lis li {
position: relative;
margin-bottom: 10px;
height: 40px;
line-height: 40px;
border-bottom: 1px solid rgb(2, 2, 2);
}
.lis li i {
position: absolute;
right: 20px;
top: 0;
text-decoration:dotted;
cursor: pointer;
}
.complete {
display: flex;
justify-content: space-between;
width: 750px;
margin-left: 20px;
}
.strickout{
text-decoration: line-through;
}
- js部分
//1先获取本地数据 三元判断 如果没有数据就给个空数组
let arr = localStorage.getItem('SY-data')? JSON.parse(localStorage.getItem('SY-data')) : []
// 2 输入代办事项 渲染到页面
//2.1定义渲染模板函数
function rander() {
let arrAll = arr.map((item,index)=>{
return `
<li class="${item.flag?'strickout':''}">
<input ${item.flag?'checked':''} type="checkbox" class='ck'style='margin:10px 0' data-id='${index}'>
<span>${item.text}</span><i data-id='${index}'>X</i></li>
`
})
document.querySelector('.lis').innerHTML = arrAll.join('')
}
//2.2获取表单
const inputVal = document.querySelector('input')
//2.2绑定Enter键盘按下事件
window.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
//非空判断
if(inputVal.value === ''){
return alert('请输入待办事项')
}
//定义一个对象保存输入的数据 flag来判断选中和不选中 默认不选中为false
let obj = {
text: inputVal.value,
flag: false
}
arr.push(obj) //添加到数组
rander() //调用渲染模板
// console.log(arr);
inputVal.value = '' //清空输入框
record()
}
})
// 3 勾选代办事项 勾选表示已完成 注意:一开始lis不存在li是通过后来添加的 这里必须要有事件委托的原理 给勾选框,删除X注册事件
document.querySelector('.lis').addEventListener('click', function (e) {
if (e.target.tagName === 'INPUT') {
// console.log(e.target.checked);
//通过e.target.checked值选中为true 不选中为false 判断改变arr对应的flag的值
if(e.target.checked){
arr[e.target.dataset.id].flag = e.target.checked
record()
rander()
}else{
arr[e.target.dataset.id].flag = e.target.checked
}
record()
rander()
}
//删除
if(e.target.tagName === 'I'){
arr.splice(e.target.dataset.id,1)
console.log(e.target.dataset.id);
record()
rander()
}
})
//4 记录未完成已完成列表
const comLeft = document.querySelector('.complete .left') //未完成
const digital = document.querySelector('.complete .right i')//已完成
//封装一个记录函数
function record(){
let lol = arr.filter(item => item.flag === true) // 过滤已完成的
console.log(lol)
digital.innerHTML = `${lol.length}`//已完成
comLeft.innerHTML = `${arr.length-lol.length}.未完成` //未完成
}
//5清除全部已完成 实际是没有删除是过滤
document.querySelector('.complete .right span').addEventListener('click',()=>{
//5.1先过滤选中的
let lol = arr.filter(item => item.flag === true)
// console.log(lol);
//5.2 进行匹配不选中的为false的
const filterData = arr.filter(item => lol.some(ele=> item.flag != ele.flag)
)
//5.3 把不选中赋给arr 这样arr数组中就是没有完成的
arr = filterData
rander()
record()
})
record()
rander()
//6保存到本地 beforeunload界面在关闭之前会触发这个事件
window.addEventListener('beforeunload',()=>{
localStorage.setItem('SY-data', JSON.stringify(arr))
})