爱编程的设计师 2022-10-03 20:07 采纳率: 94.4%
浏览 16
已结题

为什么页面显示能动态添加到元素 而打印却没有添加上

问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
我想要达到的结果

```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>

    <style>
        .home {
            position: absolute;
            display: inline-block;
            top: 30%;
            left: 40%;
            background-color: rgb(236, 236, 236);
            padding: 20px;
            border: 1px dashed black;
            border-radius: 3px;
        }

        textarea {
            box-sizing: border-box;
            width: 350px;
            height: 50px;
            outline: none;
            resize: none;
            margin-bottom: 10px;
        }

        .item {
            box-sizing: border-box;
            position: relative;
            border: 1px solid rgb(102, 102, 102);
            border-radius: 3px;
            height: 30px;
            width: 350px;
            background-color: rgb(255, 255, 255);
            margin-bottom: 5px;
        }

        .changeBlue {
            box-shadow: 0 0 10px rgb(109, 99, 243);
        }

        .item button {
            position: absolute;
            right: 3px;
            top: 50%;
            height: 20px;
            margin-top: -10px;
            width: 60px;
            margin-right: 3px;
            border: 0px;
            border-radius: 3px;
            background-color: red;
            transition: 0.3s;
            opacity: 0;
        }

        .item span {
            line-height: 30px;
        }

        .item input {
            line-height: 30px;
        }

        .footer {
            margin-top: 10px;
            margin-left: 0px;
        }
    </style>
</head>

<body>
    <div class="home">
        <div class="title">
            <textarea class="inp"></textarea>
        </div>
        <div class="list">
            <div class="item">
                <input type="checkBox">
                <span>吃饭</span>
                <button class="del">删除</button>
            </div>
            <div class="item">
                <input type="checkbox">
                <span>睡觉</span>
                <button class="del">删除</button>
            </div>
            <div class="item">
                <input type="checkBox">
                <span>上课</span>
                <button class="del">删除</button>
            </div>
        </div>
        <div class="footer">
            <input type="checkbox" name="" id="">
            <span class="total">已选0个</span>
            <span>/</span>
            <span class="sum">总共0个</span>
        </div>
    </div>

    <script>
        let itemList = Array.from(document.querySelectorAll('.item'))
        let inp = document.querySelector('.inp')
        let list = null
        let total = 0;
        let sum = 0;
        fun()
        inp.onkeydown = function (e) {
            if (e.keyCode == '13') {
                if (this.value.trim() == '') {
                    alert('输入不能为空')
                    event.preventDefault()
                } else {
                    let newEle = document.createElement('div')
                    newEle.className = 'item'
                    newEle.innerHTML = `
                    <input type="checkBox">
                    <span>${this.value}</span>
                    <button class="del">删除</button>`
                    console.log(newEle);
                    this.parentNode.appendChild(newEle)
                    itemList = Array.from(document.querySelectorAll('.item'))
                    fun()
                    this.value = ''
                    event.preventDefault()
                }

            }
        }
        function fun() {
            for (let i = 0; i < itemList.length; i++) {
                itemList[i].onmouseenter = function (e) {
                    for (let j = 0; j < itemList.length; j++) {
                        if (i == j) {
                        } else {
                            itemList[j].querySelector('.del').style.opacity = '0'
                        }
                    }
                    let ele = document.querySelector('.changeBlue')
                    if (ele == null) {
                    } else {
                        ele.classList.remove('changeBlue')
                    }
                    this.classList.add('changeBlue')
                    this.querySelector('.del').style.opacity = '1'
                }
                itemList[i].onmouseleave = function () {
                    this.classList.remove('changeBlue')
                }
            }
            for (let i = 0; i < itemList.length; i++) {
                itemList[i].onclick = function (e) {
                    if (e.target.tagName == 'BUTTON') {
                        e.target.parentNode.parentNode.removeChild(e.target.parentNode)
                    }
                    list=document.querySelector('.list')
                    sum = document.querySelectorAll('.item').length
                    total = document.querySelectorAll('input').length-1
                    console.log(sum, total);
                    console.log(list);
                }

            }
            sum = document.querySelectorAll('.item').length
            total = document.querySelectorAll('input').length-1
            console.log(sum, total);
            console.log(document.querySelector('.list'));
        }
    </script>
</body>

</html>

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/567939897466194.png )




![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/636449897466123.png )

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/248299897466169.png "#left")

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/984799897466190.png "#left")

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/040980997466177.png "#left")
  • 写回答

1条回答 默认 最新

  • MAXLZ 2022-10-03 20:34
    关注

    新增的位置不对。this.parentNode.appendChild(newEle)这里添加到.title下了,没有添加到.list下。

    const list = document.querySelector('.list')
    list.insertBefore(newEle, list.firstChild)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月11日
  • 已采纳回答 10月3日
  • 修改了问题 10月3日
  • 修改了问题 10月3日
  • 展开全部

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看