js做留言板(可以评论 删除评论 评论时间)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            padding:0;margin: 0;
        }
        .wrap{
            width: 800px;border: 1px solid black;
            padding: 20px;margin: 0 auto;
        }
        .wrap h3{
            margin: 10px 0 20px;
        }
        .message{
            padding: 10px;border: 1px dashed #ccc;
        }
        .info{
            display: flex;justify-content: space-between;
            border-bottom: 1px dashed #ccc;
            padding-bottom: 10px;
            margin-bottom: 10px;
        }
        .content{
            width: calc(100%-2px);
            height: 120px;
            border-radius: 5px;border-color: #ccc;
            vertical-align: middle;
            margin: 10px 0;
        }
        .btn{
            text-align: right;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <h3>留言板</h3>
        <div class="exhibit" id="exhibit"></div>
        <div class="messageText">
             <textarea class="content" id="content"></textarea>
             <div class="btn">
                 <input type="button" value="留言" id="addBtn">
             </div>
             </div>
    </div>

    <script src="index.js"></script>
</body>
</html>

  

let data = [{
    username:"张三0",
    time:"2017-09-07 09:11:00",
    text:"留言内容0"},{
        username:"张三1",
        time:"2017-09-08 09:11:00",
    text:"留言内容0"},{
        username:"张三2",
        time:"2017-09-09 09:11:00",
    text:"留言内容0"
}];

render();
addBtn.addEventListener("click",() =>{
    addMessage();
})

content.addEventListener("keydown",(e) =>{
    if (e.keyCode === 13){
        addMessage();
    }
},false)

exhibit.addEventListener("click" , (e) =>{
    console.log(e)
    if (e.target.className === "removeBtn"){
        // e.preventDefault();
        e.preventDefault();
        let i =e.target.getAttribute("_id");
        data.splice(i,1);
        render();
    }
}, false)

function addMessage(){
    data.push({
        username:`张三${data.length}`,
        time:getNowTime(),
        text:content.value
    });
    content.value = "";
    render();
}

function render(){
    exhibit.innerHTML = data.map((item, index) =>{
                return `<div class="message">
                <div class="info">
                    <span>${item.username}</span>
                    <span>${item.time}</span>
                </div>
                <p>${item.text}</p>
                <div class="btn">
                    <a href="#" class = "removeBtn" _id="${index}">删除</a>
                </div>
                </div>`
    }).join("");
}

function getNowTime(){
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    month =judgeTime(month);
    let day = date.getDate();
    day = judgeTime(day);
    let hours = date.getHours();
    hours = judgeTime(hours);
    let minutes = date.getMinutes();
    minutes = judgeTime(minutes);
    let seconds = date.getSeconds();
    seconds = judgeTime(seconds);
    let nowTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    return nowTime;
}

function judgeTime(time){
    if(time < 10 && time > 0){
        time = "0" + time;
    }
    return time;
}

  

转载于:https://www.cnblogs.com/gao2/p/11528682.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值