案例-任务清单

文章目录

效果展示

初始化面

演示画面

任务清单

代码区

html 复制代码
<!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>
相关推荐
徐小夕18 小时前
我用 AI 撸了个开源"万能预览器":浏览器直接打开 Office、CAD 和 3D 模型
前端·vue.js·github
小码哥_常18 小时前
Flutter Android 延迟加载代码指南:提升应用性能的关键
前端
这是个栗子18 小时前
TypeScript(三)
前端·javascript·typescript·react
kvo7f2JTy18 小时前
基于机器学习算法的web入侵检测系统设计与实现
前端·算法·机器学习
北风toto19 小时前
前端CSS样式详细笔记
前端·css·笔记
nanfeiyan19 小时前
git commit
前端
前端精髓21 小时前
移除 Effect 依赖
前端·javascript·react.js
码云之上21 小时前
从一个截图函数到一个 npm 包——pdf-snapshot 的诞生记
前端·node.js·github
码事漫谈1 天前
AI提效,到底能强到什么程度?
前端·后端
IT_陈寒1 天前
React hooks依赖数组这个坑差点把我埋了
前端·人工智能·后端