我正试图创建一个包含聊天框的仪表盘。我在网上找了很多资源,用bootstrap想出了一个简单的UI。
<!DOCTYPE html>
<html>
<head>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<style type="text/css">
.chat
{
list-style: none;
margin: 0;
padding: 0;
}
.chat li
{
margin-bottom: 10px;
padding-bottom: 5px;
border-bottom: 1px dotted #B3A9A9;
}
.chat li.left .chat-body
{
margin-left: 60px;
}
.chat li.right .chat-body
{
margin-right: 60px;
}
.chat li .chat-body p
{
margin: 0;
color: #777777;
}
.panel .slidedown .glyphicon, .chat .glyphicon
{
margin-right: 5px;
}
.panel-body
{
overflow-y: scroll;
height: 250px;
}
::-webkit-scrollbar-track
{
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
background-color: #F5F5F5;
}
::-webkit-scrollbar
{
width: 12px;
background-color: #F5F5F5;
}
::-webkit-scrollbar-thumb
{
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
background-color: #555;
}
</style>
</head>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<div class="col-md-5">
<div class="panel panel-primary">
<div class="panel-heading" id="accordion">
<span class="glyphicon glyphicon-comment"></span> Chat
<div class="btn-group pull-right">
<a type="button" class="btn btn-default btn-xs" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
<span class="glyphicon glyphicon-chevron-down"></span>
</a>
</div>
</div>
<div class="panel-collapse collapse" id="collapseOne">
<div class="panel-body">
<ul class="chat">
<li class="left clearfix"><span class="chat-img pull-left">
<img src="http://placehold.it/50/55C1E7/fff&text=U" alt="User Avatar" class="img-circle" />
</span>
<div class="chat-body clearfix">
<div class="header">
<strong class="primary-font">Jack</strong>
</div>
<p>
Comment 1.
</p>
</div>
</li>
<li class="left clearfix"><span class="chat-img pull-left">
<img src="http://placehold.it/50/FA6F57/fff&text=ME" alt="User Avatar" class="img-circle" />
</span>
<div class="chat-body clearfix">
<div class="header">
<strong class="primary-font">Harry</strong>
</div>
<p>
Harry - Comment
</p>
</div>
</li>
<li class="left clearfix"><span class="chat-img pull-left">
<img src="http://placehold.it/50/FA6F57/fff&text=ME" alt="User Avatar" class="img-circle" />
</span>
<div class="chat-body clearfix">
<div class="header">
<strong class="primary-font">Harry</strong>
</div>
<p>
Harry - Comment2
</p>
</div>
</li>
</ul>
</div>
<div class="panel-footer">
<div class="input-group">
<input id="btn-input" type="text" class="form-control input-sm" placeholder="Type your message here..." />
<span class="input-group-btn">
<button class="btn btn-warning btn-sm" id="btn-chat">
Send</button>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
这里每个 利 元素有如下格式的html代码。
<li class="left clearfix"><span class="chat-img pull-left">
<img src="http://placehold.it/50/FA6F57/fff&text=ME" alt="User Avatar" class="img-circle" />
</span>
<div class="chat-body clearfix">
<div class="header">
<strong class="primary-font">Harry</strong>
</div>
<p>
Some Comment.
</p>
</div>
</li>
我试图从数据库中预先填充过去的评论。我得到了一个按时间顺序排列的名字和评论的地图。例如,这个地图可以是这样的{Jack: Comment1, Harry: Comment2... }
我想动态地填写 p 签注 强势 标签,谁能帮帮我?
使用一个模板 。
<template id="comment-template">
<li class="left clearfix">
<span class="chat-img pull-left">
<img src="http://placehold.it/50/FA6F57/fff&text=ME" alt="User Avatar" class="img-circle" />
</span>
<div class="chat-body clearfix">
<div class="header">
<strong class="primary-font"></strong>
</div>
<p></p>
</div>
</li>
</template>
<div id="comments-cnt"></div>
我假设数据是类似于 。
const comments = [
{ jack: 'comment1' },
{ harry: 'comment2' },
{ david: 'comment3' }
]
然后在这个数组上迭代,克隆模板,用名称填充它,从每个对象文字注释,并将其插入到容器中。
let commentCnt = document.getElementById('comments-cnt')
const template = document.getElementById('comment-template')
comments.forEach(function(c) {
//extract data
const name = Object.keys(c)[0]
const comment = c[name]
//create clone of the template and populate
let clone = document.importNode(template.content, true)
clone.querySelector('strong').innerText = name
clone.querySelector('p').innerText = comment
//insert
commentCnt.appendChild(clone)
})
你的结构是 <li>
元素模板使其非常容易。