摘要:<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="
<!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>在线客服系统</title>
<style type="text/css">
div:nth-child(1) {
width: 450px;
height: 650px;
background-color: lightskyblue;
margin: 30px auto;
color: #333;
box-shadow: 2px 2px 2px #808080
}
h2 {
text-align: center;
margin-bottom: -10px;
}
div:nth-child(2) {
width: 400px;
height: 500px;
border: 4px double green;
background-color: #efefef;
margin: 20px auto 10px;
}
ul {
list-style: none;
line-height: 2em;
/*border: 1px solid red;*/
overflow: hidden;
padding: 15px;
}
table {
width: 90%;
height:80px;
margin: auto;
}
textarea{
/*width: 300px;*/
border: none;
resize: none;
background-color: lightyellow;
}
button {
width: 60px;
height: 40px;
background-color: seagreen;
color: white;
border: none;
/*text-align: left;*/
}
button:hover {
cursor: pointer;
background-color: orange;
}
</style>
</head>
<body>
<div>
<h2>在线客服</h2>
<div contenteditable="true">
<ul>
<li></li>
</ul>
</div>
<table>
<tr>
<td align="right"><textarea cols="50" rows="4" name="text"></textarea></td>
<td align="left"><button type=button>发送</button></td>
</tr>
</table>
</div>
<script type="text/javascript">
let btn = document.getElementsByTagName('button')[0]; //获取到按钮
let text = document.getElementsByName('text')[0]; //获取到文本
let list = document.getElementsByTagName('ul')[0]; //获取到对话框内容
let sum = 0; //计数量
btn.onclick = function () { //按钮点击事件
if (text.value.length === 0) { //如果输入内容为空
alert('客官:是不是忘记输入内容了~~'); 内容为空 //提示
return false;
}
let userComment = text.value; //获取要发发送的文本内容
text.value = ''; //清空输入框内容
let li = document.createElement('li'); //创建一个新节点li
li.innerHTML = userComment; //将用户输入的内容添加到新节点中
let userPic = '<img src="http://img2.imgtn.bdimg.com/it/u=4040262769,939356189&fm=26&gp=0.jpg" width="30" style="border-radius:50%">'; // 用户头像
li.innerHTML = userPic + ' ' + userComment; // 将用户头像与用户的聊天内容合并
list.appendChild(li); //发送聊天内容,实际上就是将新节点插入到对话列表中
sum += 1; // 聊天记录自增1
setTimeout(function(){ //设置定时器,默认2秒后会自动回复
let info = [
'真烦人,有话快说,别耽误我玩抖音',
'除了退货,退款,咱们什么都可以聊',
'说啥,本姑娘怎么听不懂呢?再说一遍',
'在我方便的时候再回复你吧~~',
'投诉我的人多了,你算老几~~~'
];
let temp = info[Math.floor(Math.random()*3)]; //取1-5之间的一个整数:Math.floor(Math.random()*6 + 1)
let reply = document.createElement('li'); //创建一个新节点li
let kefuPic = '<img src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2875324640,1591537022&fm=26&gp=0.jpg" width="30" style="border-radius:50%;">'; //客服头像
reply.innerHTML = kefuPic + ' ' + '<span style="color:red">'+temp+'</span>'; //客服头像+回复内容
list.appendChild(reply); //输出内容
sum += 1;
},2000);
// 当聊天记录达到10条时清空窗口
if (sum > 10) {
list.innerHTML = '';
sum = 0;
}
}
</script>
</body>
</html>
批改老师:韦小宝批改时间:2019-02-19 09:10:06
老师总结:嗯 写的很不错 这个项目不仅仅让我们复习了之前学习的HTML以及css的布局还练习了js方面的知识 没事的时候自己可以再把这个项目再来完善完善