<body style="padding: 15px;"><!-- 评论面板 --><div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title">发表评论</h3></div><form class="panel-body" id="formAddCmt"><div>评论人:</div><input type="text" class="form-control" name="username" autocomplete="off" /><div>评论内容:</div><textarea class="form-control" name="content"></textarea><button type="submit" class="btn btn-primary">发表评论</button></form></div><!-- 评论列表 --><ul class="list-group" id="cmt-list"><li class="list-group-item"><span class="badge" style="background-color: #F0AD4E;">评论时间:</span><span class="badge" style="background-color: #5BC0DE;">评论人:</span>Item 1</li></ul></body>
注意:此处自行下载 bootstrap 文档 进行引入
<link rel="stylesheet" href="./lib/bootstrap.css" />
注意:此处自行下载 jQuery 进行引入
<script src="./lib/jquery.js"></script>function getCommentList() {$.ajax({method: 'GET',url: 'http://www.liulongbin.top:3006/api/cmtlist',success: function (res) {if (res.status !== 200) return alert('获取评论列表失败!')var rows = []$.each(res.data, function (i, item) {var str = '<li class="list-group-item"><span class="badge" style="background-color: #F0AD4E;">评论时间:' + item.time + '</span><span class="badge" style="background-color: #5BC0DE;">评论人:' + item.username + '</span>' + item.content + '</li>'rows.push(str)})$('#cmt-list').empty().append(rows.join(''))}})
}getCommentList()$(function () {$('#formAddCmt').submit(function (e) {e.preventDefault()var data = $(this).serialize()$.post('http://www.liulongbin.top:3006/api/addcmt', data, function (res) {if (res.status !== 201) {return alert('发表评论失败!')}getCommentList()$('#formAddCmt')[0].reset()})})
})function template(id, data) {var str = document.getElementById(id).innerHTMLvar pattern = /{{\s*([a-zA-Z]+)\s*}}/var pattResult = nullwhile (pattResult = pattern.exec(str)) {str = str.replace(pattResult[0], data[pattResult[1]])}return str
}