简单的写一个评论区

这是一个网页评论系统的实现,包括输入限制、动态显示剩余字数、发布按钮、编辑和删除功能。用户输入评论后,可以编辑已发布的评论或者删除不再需要的评论。系统使用JavaScript进行动态操作,创建并插入新的评论元素到页面上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!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>
        * {
            margin: 0;
            padding: 0;
        }

        #inputText {
            width: 500px;
            height: 40px;
        }

        #publish {
            width: 40px;
            height: 30px;
            margin-left: 300px;
            background-color: #fff;
            border: 1px solid #666;
            outline: none;
            color: #999;
        }

        .pub {
            margin-top: 30px;
        }

        #box {
            width: 500px;
            /* height: 400px; */

            margin-top: 50px;
            position: relative;
        }

        #box button:nth-child(2) {
            width: 40px;
            height: 20px;
            position: absolute;
            margin-left: 400px;
            top: 10px;
        }

        #box div {
            position: relative;
            margin: 20px 0px;

            background-color: #999;
            height: 40px;
            line-height: 40px;
            
        }

        #box button:nth-child(3) {
            position: absolute;
            width: 40px;
            height: 20px;
            top: 10px;
            margin-left: 450px;
        }

        #box span {
            position: absolute;
        }

        #amendText input {
            width: 400px;
            height: 80px;
            border: 1px solid #ccc;
            outline: none;
        }

        #amendText button {
            width: 40px;
            height: 20px;
        }

        #amendText {
            position: absolute;
            top: 100px;
            left: 300px;
        }

        #amendText button {
            position: absolute;
            top: 63px;
            left: 405px;
        }
    </style>
</head>

<body>
    <h2>网友评论</h2>
    <p>
        <input type="text" id="inputText" placeholder="我有话想说...">
    </p>
    <p class="pub">
        还可以输入<span id="num">20</span>个字
        <input type="button" value="发布" id="publish">
    </p>
    <div id="box"></div>
    <div id="amendText"></div>
    <script>
        var inputText = document.getElementById('inputText');
        var num = document.getElementById('num');
        var box = document.getElementById('box');
        var amendText = document.getElementById('amendText');

        inputText.oninput = function () {
            if (inputText.value.length > 20) {
                inputText.value = inputText.value.slice(0, 20);

            }
            num.innerText = 20 - inputText.value.length;
            publish.style.color = 'black';
        }
        publish.onclick = function () {
            var newDiv = document.createElement('div');
            var compile = document.createElement('button');
            var amend = document.createElement('button');
            var newSpan = document.createElement('span');
            compile.innerText = '编辑';
            amend.innerText = '删除';
            newSpan.innerText = inputText.value;
            console.log(newSpan);
            newDiv.appendChild(newSpan);
            newDiv.appendChild(compile);
            newDiv.appendChild(amend);
            //将后面输入的内容插入先输入内容的前面
            if(box.children.length==0){
                box.appendChild(newDiv);
            }else{
                box.insertBefore(newDiv,box.firstElementChild);
            }
            inputText.value = ''
            num.innerText = 20;
            publish.style.color = '#999';
            box.style.border = '2px solid #ccc'
        //    box.style.display='none'
            //编辑内容
            compile.onclick = function () {
                var pNew = document.createElement('input');
                var butt = document.createElement('button');
                butt.innerText = '修改';
                pNew.value = newSpan.innerText;
                amendText.appendChild(pNew);
                amendText.appendChild(butt);
                butt.onclick = function () {
                    newSpan.innerText = pNew.value;
                    amendText.style.display = 'none';
                    amendText.innerHTML = '';
                }
                pNew.oninput = function () {
                        if (pNew.value.length > 20) {
                            pNew.value = pNew.value.slice(0, 20);
                        }
                    }
                amendText.style.display = 'block';
            }
            // 删除内容
            amend.onclick = function () {
                box.removeChild(this.parentNode);
                if(box.children.length==0){
                    box.style.display='none';
                }
            }
            box.style.display='block';
        }

    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值