文章目录
- 效果展示
- 初始化面
- 演示画面
- 代码区
效果展示
初始化面
演示画面
任务清单
代码区
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>任务清单</title><style>css">* {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>javascript">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.valueconsole.dir(inputTask)console.log(inputMessage)if (inputMessage == "") return// 赋值spanSon.innerHTML = inputMessagebutSon.innerHTML = "删除"// 插入父节点中去checkSon.type = "checkbox"divParent.appendChild(checkSon)divParent.appendChild(spanSon)divParent.appendChild(butSon)console.log(divParent)// cssdivParent.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.parentNodelet grandNode = parentNode.parentNodegrandNode.removeChild(parentNode)}}// 遍历复选框let buttonSelectAll = document.querySelectorAll(".contain input")for (i = 0; i < buttonSelectAll.length; ++i) {buttonSelectAll[i].onclick = function () {let selectValue = this.checkedlet parentNode = this.parentNodeif (selectValue) {// true 插入完成列表ready.appendChild(parentNode)} else {// falsedisready.appendChild(parentNode)}}}}</script></html>