html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="jquery-3.1.0.js"></script>
<style>
#main{
width: 960px;
margin: 0 auto;
}
.apply div{
display: flex;
align-items: center;
margin: 10px;
}
.apply div label{
width: 100px;
}
.apply-item{
margin: 25px 25px 25px 10px;
}
</style>
</head>
<body>
<div id="main">
<div id="content">
<h2>你们坚持了五年以上的习惯是什么?</h2>
<p>把自己和比自己强的人绑定在一起。利益永远都是社交的本质,只有傻子才为了社交而社交,我很市侩,也很现实,如果我不能和比我强的人绑定在一起,我就无法意识到自己的不足,也就无法进步。近朱者赤,近墨者黑。所以如果有机会接触到牛人,一定要尽力拉近关系,如果能有幸和牛人来一次畅谈,对方的观点和见解,可能让你有【胜读十年书】的震撼。绑定别人最好的方式就是利益相关,一起创业合作,能把对方套的死死的。</p>
</div>
<div id="msg">
<div class="apply">
<div>
<label for="nickName">评论人</label>
<input type="text" name="nickName" id="nickName" placeholder="请输入您的昵称">
</div>
<div>
<label for="app">评论内容</label>
<textarea name="app" id="app" cols="30" rows="10"></textarea>
</div>
<div>
<label></label>
<button type="button" id="sub">提交</button>
</div>
</div>
<div class="apply-list">
</div>
</div>
</div>
</body>
<script>
$(function () {
getList();
$("#sub").click(function () {
let nickName=$("#nickName").val();
if(!nickName){
alert("昵称不能为空!");
return;
}
let app=$("#app").val();
if(!app){
alert("评论内容不能为空!");
return;
}
let time=new Date().toLocaleString();
$("input,textarea").val("");
let appList=localStorage.getItem("appList") || "[]";
appList=JSON.parse(appList);
let appObj={
nickName:nickName,
app:app,
time:time
};
appList.push(appObj);
appList=JSON.stringify(appList);
localStorage.setItem("appList",appList);
getList();
});
});
function getList(){
let appList=localStorage.getItem("appList") || "[]";
appList=JSON.parse(appList);
appList=appList.reverse();
let html='';
for(let i=0;i<appList.length;i++){
let obj=appList[i];
html+='<div class="apply-item"><div class="item-nickname">昵称:'+obj.nickName+'</div><div class="item-content">'+obj.app+'</div><div class="item-time">'+obj.time+'</div></div>';
}
$('.apply-list').empty().append(html);
}
</script>
</html>