abstract:<!-- 本次作业再次完善了在线客服聊天系统。主要增加了,1、禁止发送空消息功能(直接通过if语句判断Input.value是否为空);2、在消息内容前面增加了头像;3、通过一个字符串数组预置一些聊天语句,利用settimeout()函数定时通过Math.fllor(Math.random())方法进行随机读取聊天语句,拼接上头像文件在指定的ul li中显示。同时记录li的数量,在达到10条
<!-- 本次作业再次完善了在线客服聊天系统。主要增加了,1、禁止发送空消息功能(直接通过if语句判断Input.value是否为空);2、在消息内容前面增加了头像;3、通过一个字符串数组预置一些聊天语句,利用settimeout()函数定时通过Math.fllor(Math.random())方法进行随机读取聊天语句,拼接上头像文件在指定的ul li中显示。同时记录li的数量,在达到10条消息时清空ul中的内容,并重置num变量为0。 -->
<!DOCTYPE html>
<html>
<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>Chating_Room_Case</title>
<style>
* {
padding: 0;
margin: 0;
}
.mess {
width: 100%;
text-align: left;
}
ul li {
list-style: none;
padding: 4px;
line-height: 5px;
}
.container {
margin: 0 auto;
text-align: center;
margin: 15px;
}
.container .chatbox {
margin: 0 auto;
width: 300px;
height: 450px;
border: 1px solid #ccc;
}
.container .chatbox span {
display: block;
width: 100%;
color: #fff;
background: #5c28d4;
}
.container .btn {
margin: 0 auto;
width: 120px;
height: 45px;
color: #fff;
letter-spacing: 12px;
background: rgb(26, 121, 199);
border-radius: 5px;
text-align: center;
}
.container .msg {
height: 40px;
border: 1px solid #ddd;
padding: 0 5px;
}
</style>
</head>
<body>
<div class="container">
<div id="cbox" class="chatbox">
<span style="font-size:20px;">Welcome to this Chatroom</span>
<hr>
<div class="mess">
<ul>
<li></li>
</ul>
</div>
</div>
<div>
<input type="text" placeholder="欢迎来到本聊天室......." class="msg"> <button class="btn">发送</button></div>
</div>
<script>
let input = document.getElementsByClassName('msg')[0];
let ul = document.getElementsByTagName('ul')[0];
let button = document.getElementsByClassName('btn')[0];
let num = 0;
button.onclick = function () {
if (input.value.length == 0) {
alert('hello,please talking');
return false;
}
let li = document.createElement('li');
let avandor = '<img src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=215857342,2013279815&fm=26&gp=0.jpg" style="width:30px;height:30px;border-radius:50%;">';
li.innerHTML = avandor + ' ' + input.value;
ul.appendChild(li);
input.value = '';
num += 1;
setTimeout(function () {
let msg = [
'hello,this is my chating room',
'Nani,what do you say?',
'I like drink orange juice',
'how do you do?'
];
let tmp = msg[Math.floor(Math.random() * 4)];
let reply = document.createElement('li');
let repic = '<img src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2519058618,822923318&fm=26&gp=0.jpg" style="width:30px;height:30px;border-radius:50%;">';
reply.innerHTML = repic + ' ' + tmp;
ul.appendChild(reply);
num += 1;
}, 1000);
if (num > 10) {
ul.innerHTML = '';
num = 0;
}
}
</script>
</body>
</html>
Correcting teacher:韦小宝Correction time:2019-03-09 13:29:25
Teacher's summary:和上一篇作业基本上是一样的 加了个总结描述 写的还是很不错的