利用js实现留言板模块效果

相信大家都遇见过留言板这个东西,一般来说开发过程中都会要求做这个模块,这里我把留言板的基本的功能做了一下,后续大家可以在里面添加更多的功能.
成果图展示
在这里插入图片描述
代码也在这,样式可以自己按照喜欢的改就行了,这里的样式只是一个成品,希望大家不要介意

<style>
        .box{
            position: relative;
            margin: 100px auto;
            width: 300px;
            height: 500px;
            /* border: 1px solid red; */
            background-color: #95a5a6;
        }
        .box h4{
            text-align: center;
            color: white;
        }
        .box button{
            background-color: #1abc9c;
            position: absolute;
            right: 10px;
            top: 100px;   
            outline: none;
            color: white;
            border: 0;
            cursor: pointer;
        }
        .box textarea{
            outline: none;
            resize: none;
            margin-top: 20px;
            margin-left: 30px;
            width: 200px;
            height: 100px;
            color: gray;
        }
        .box li{
            height: 30px;
            width: 200px;
            line-height: 30px;
            list-style-type: none;
            background-color: #BDC581;
            padding-left: 8px;
            color: white;
            border-radius: 5px;
            margin-top: 5px;
        }
    </style>
--------------html------------------------
<body>
    <div class="box">
        <h4>留言板</h4>
    <textarea rows="30" cols="20" value=""></textarea>
    <button>留言</button>
    <ul></ul>
    </div>  
    --------------JS模块-------------------
    <script>
        var btn = document.querySelector("button");
        var txt = document.querySelector("textarea");
        var ul = document.querySelector("ul");
        
        // console.log(btn);
        // console.log(txt);
        btn.onclick = function(){
            if(txt.value == ""){
                alert("您还没有输入内容哦");
                return false;
            }else{
            //创建元素
            var li =document.createElement("li");
            //添加元素
            li.innerHTML =txt.value;
            ul.appendChild(li); //如果想添加在前面显示就可以用insertBefore();
            }
            txt.value = "";
        }
    </script>
  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值