评论提交

2022-03-09  本文已影响0人  心存美好

评论提交(不采用节流,用户没有输入内容就不提交,innerHTML方式)

<!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>
        body {
            font-family: "Microsoft YaHei", serif;
            user-select: none
        }

        body,
        dl,
        dd,
        p,
        h1,
        h2,
        h3,
        h4,
        h5,
        h6 {
            margin: 0;
        }

        ol,
        ul,
        li {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        img {
            border: none;
        }

        #wrap {
            width: 500px;
            margin: 50px auto;
            background-color: pink;
        }

        #text {
            width: 494px;
            height: 100px;
            border: 1px solid #000;
            resize: none;
            /* 让textarea不可以调整大小 */
        }

        #inp {
            float: right;
        }

        #list {
            width: 100%;
            margin-top: 30px;
        }

        #list li {
            width: 100%;
            line-height: 22px;
            font-size: 14px;
            color: #333;
            text-indent: 2em;
            padding: 10px 0;
            border-bottom: 1px dotted #999;
        }
    </style>
</head>

<body>
    <div id="wrap">
        <textarea id="text"></textarea>
        <input type="text" id="inp" value="提交">
        <ul id="list">
            <li>11</li>
            <li>33</li>
            <li>44</li>
            <li>66</li>
            <li>66</li>
        </ul>
    </div>
    <script>
        //不采用节流的方式
        //获取元素
        let oText = document.getElementById('text'), oBtn = document.getElementById('inp'), oList = document.getElementById('list')
        //绑定函数
        oBtn.onclick = function () {
            console.log(11111);
            let val = oText.value.trim()  //获取内容
            if (val) {
                oList.innerHTML += `<li>${val}</li>`   //将内容加到页面上innerHMTL方式
                oText.value = ''//清空表单
            }
        }
    </script>
</body>

</html>

采用节流方式:用户没有输入内容时,有个弹框提示 innerHTML方式

<!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>
        body {
            font-family: "Microsoft YaHei", serif;
            user-select: none
        }

        body,
        dl,
        dd,
        p,
        h1,
        h2,
        h3,
        h4,
        h5,
        h6 {
            margin: 0;
        }

        ol,
        ul,
        li {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        img {
            border: none;
        }

        #wrap {
            width: 500px;
            margin: 50px auto;
            background-color: pink;
        }

        #text {
            width: 494px;
            height: 100px;
            border: 1px solid #000;
            resize: none;
            /* 让textarea不可以调整大小 */
        }

        #inp {
            float: right;
        }

        #list {
            width: 100%;
            margin-top: 30px;
        }

        #list li {
            width: 100%;
            line-height: 22px;
            font-size: 14px;
            color: #333;
            text-indent: 2em;
            padding: 10px 0;
            border-bottom: 1px dotted #999;
        }
    </style>
</head>

<body>
    <div id="wrap">
        <textarea id="text"></textarea>
        <input type="text" id="inp" value="提交">
        <ul id="list">
            <li>11</li>
            <li>33</li>
            <li>44</li>
            <li>66</li>
            <li>66</li>
        </ul>
    </div>
    <script>
        //采用节流方式:用户没有输入内容时,有个弹框提示
        //获取元素
        let oText = document.getElementById('text'), oBtn = document.getElementById('inp'), oList = document.getElementById('list')
        //绑定函数
        oBtn.onclick = function () {
            let val = oText.value.trim()  
            if (!val) {
                alert('请输入内容')
                return;   //输入为空时就跳过后面的代码,就是节流
            }
                oList.innerHTML += `<li>${val}</li>`   //将内容加到页面上innerHTML方式插入
                oText.value = ''//清空表单
           
        }
    </script>
</body>

</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>
        body {
            font-family: "Microsoft YaHei", serif;
            user-select: none
        }

        body,
        dl,
        dd,
        p,
        h1,
        h2,
        h3,
        h4,
        h5,
        h6 {
            margin: 0;
        }

        ol,
        ul,
        li {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        img {
            border: none;
        }

        #wrap {
            width: 500px;
            margin: 50px auto;
            background-color: pink;
        }

        #text {
            width: 494px;
            height: 100px;
            border: 1px solid #000;
            resize: none;
            /* 让textarea不可以调整大小 */
        }

        #inp {
            float: right;
        }

        #list {
            width: 100%;
            margin-top: 30px;
        }

        #list li {
            width: 100%;
            line-height: 22px;
            font-size: 14px;
            color: #333;
            text-indent: 2em;
            padding: 10px 0;
            border-bottom: 1px dotted #999;
        }
    </style>
</head>

<body>
    <div id="wrap">
        <textarea id="text"></textarea>
        <input type="text" id="inp" value="提交">
        <ul id="list">
        </ul>
    </div>
    <script>
        //采用节点操作
        //获取元素
        let oText = document.getElementById('text'), oBtn = document.getElementById('inp'), oList = document.getElementById('list')
        //绑定函数
        oBtn.onclick = function () {
            console.log(111);
            let val = oText.value.trim()
            if (!val) {
                alert('请输入内容')
                return;   //输入为空时就跳过后面的代码
            }
            let oLi = document.createElement('li')       //采用节点操作
            oLi.innerHTML = val
            if (oList.children) {     //有子节点,就在第一个子节点前插入
                oList.insertBefore(oLi, oList.children[0])
            } else {
                oList.appendChild(oLi)
            }
            oText.value = ''

        }
    </script>
</body>

</html>
上一篇 下一篇

猜你喜欢

热点阅读