留言板的javascript写法

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

    <style>

        *{margin:0;padding:0;}

        div{width:500px;

        height:auto!important;

        min-height: 800px;

        height:800px;

        margin: 50px auto;

            -webkit-user-select: none;

            -moz-user-select: none;

            -ms-user-select: none;

            user-select: none;

        }

        #txt{

            width:440px;

            height:169px;

        }

        #list{

        width:400px;

        height:auto!important;

            min-height:400px;

            height:400px;

            border:1px solid #ccc;

            list-style: none;

        }

        li{

        border-bottom: 1px solid #ccc;

        overflow: hidden;

        margin-bottom: 8px;

        height:45px;}

        p{

        font-size:12px;

        color:#f5e79e;

        float:right;}

        b{

        font-size:20px;

        color:red;}

        span{

        margin-bottom: 5px;

        }

    </style>

</head>

<body>

    <div>

        输入你的名字:<input type="text" id="Name">

        <textarea name="" id="txt" cols="30" rows="10">

        </textarea><br>

        <input type="button" value="添加" id="add">

        <input type="button" value="删除" id="clear">

        <ul id="list"></ul>

    </div>

</body>

<script>

    var txt=document.getElementById("txt");

    var add=document.getElementById("add");

    var clear=document.getElementById("clear");

    var list=document.getElementById("list");

    var Name=document.getElementById("Name");

    //Name.value+txt.value



    data();

    add.onclick=function(){

        var time=new Date();

        var val=Name.value+"|"+txt.value;//string

        console.log(val);

        localStorage.setItem(time,val);

        console.log(localStorage.getItem(time));

        data();

    };

    clear.onclick=function(){

        localStorage.clear();

        list.innerHTML="";

        txt.value="";

    };

    function data(){


        var str="";

        //先存取数据

        console.log(localStorage.length);//3

        for(var i=0;i<localStorage.length;i++){

            var keys=localStorage.key(i);//获取循环下所有key值;

            console.log(keys);

            //取到value

            var value=localStorage.getItem(keys);//value  Name.value+"|"+txt.value;

            if(value.split("|")[0]==""){

                str+="<li><b>匿名人</b>:<span>"+value.split("|")[1]+"</span><p>"+keys+"</p></li>"

            }else{

                str+="<li><b>"+value.split("|")[0]+"</b>:<span>"+value.split("|")[1]+"</span><p>"+keys+"</p></li>"

            }


        }

        list.innerHTML=str;

        var lis=list.getElementsByTagName("li");

        for(var i=0;i<lis.length;i++){

            lis[i].index=i;

            lis[i].ondblclick=function(){

                list.removeChild(this);

                var ks=localStorage.key(this.index);

                localStorage.removeItem(ks);

            }

        }

    }

</script>

</html>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值