效果展示
初始化面
演示画面
任务清单
代码区
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>任务清单</title>
<style>
* {
margin: 0px;
padding: 0px;
}
body {
width: 400px;
margin: 0px auto;
/*上下 左右*/
}
.total {
display: flex;
}
.input {
width: 300px;
height: 50px;
margin-top: 20px;
}
.createTask {
height: 50px;
color: white;
background-color: orange;
border-color: white;
}
.createTask:hover {
background-color: grey;
}
.createTask:active {
background-color: green;
}
h3 {
width: 180px;
color: white;
background-color: black;
text-align: center;
margin-top: 5px;
}
.contain {
margin-top: 2px;
}
</style>
</head>
<body>
<div>
<input class="input" type="text"><button class="createTask" onclick="creatAssign()">新建任务</button>
</div>
<div class="total">
<div class="disready">
<h3>未完成</h3>
<!-- <div class="contain">
<input type="checkbox">
<span>测试案例</span>
<button>删除</button>
</div> -->
</div>
<div class="ready">
<h3>已完成</h3>
</div>
</div>
</body>
<script>
function creatAssign() {
// 找到祖父
let disready = document.querySelector('.disready')
let ready = document.querySelector('.ready')
// 创建父节点
let divParent = document.createElement('div')
// 创建子节点
let checkSon = document.createElement('input')
let spanSon = document.createElement('span')
let butSon = document.createElement('button')
// 获取任务输入内容
let inputTask = document.querySelector('.input')
let inputMessage = inputTask.value
console.dir(inputTask)
console.log(inputMessage)
if (inputMessage == "") return
// 赋值
spanSon.innerHTML = inputMessage
butSon.innerHTML = "删除"
// 插入父节点中去
checkSon.type = "checkbox"
divParent.appendChild(checkSon)
divParent.appendChild(spanSon)
divParent.appendChild(butSon)
console.log(divParent)
// css
divParent.className = "contain"
// 插入祖父结点
disready.appendChild(divParent)
// 遍历删除按键
let buttonDeleteAll = document.querySelectorAll(".contain button")
for (i = 0; i < buttonDeleteAll.length; ++i) {
buttonDeleteAll[i].onclick = function () {
let parentNode = this.parentNode
let grandNode = parentNode.parentNode
grandNode.removeChild(parentNode)
}
}
// 遍历复选框
let buttonSelectAll = document.querySelectorAll(".contain input")
for (i = 0; i < buttonSelectAll.length; ++i) {
buttonSelectAll[i].onclick = function () {
let selectValue = this.checked
let parentNode = this.parentNode
if (selectValue) {
// true 插入完成列表
ready.appendChild(parentNode)
} else {
// false
disready.appendChild(parentNode)
}
}
}
}
</script>
</html>