<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>模拟智能在线客服系统1</title>
<style type="text/css">
div:nth-child(1) {width: 450px;height: 650px;background-color: lightskyblue;margin: 30px auto;color: #333;box-shadow: 5px 5px 10px #808080}
h2 {text-align: center; margin-bottom: -10px;}
div:nth-child(2) {width: 400px;height: 500px;border: 4px double green;border-radius: 20px;background-color: #efefef;margin: 20px auto 10px;}
ul {list-style: none;line-height: 2em;overflow: hidden;padding: 15px;}
table {width: 400px;height:80px;margin: auto;}
textarea{border: none;resize: none; background-color: lightyellow;}
button {width: 60px;height: 40px;background-color: seagreen;color: white;border: none;}
button:hover {cursor: pointer;background-color: orange;}
</style>
</head>
<body>
<div>
<h2>在线客服</h2>
<div contenteditable="true">
</div>
<table>
<tr>
<td><textarea cols="44" rows="3" name="text"></textarea></td>
<td><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('div')[1];
let sum = 0;
//添加按钮点击事件,获取用户数据并推送到对话窗口中
btn.onclick = function () {
let usert
if (text.value.length == 0) {
usert='<span style="color:blue;">请输入内容!!!</span>'
}else{
usert = text.value;
}
//立即清空用户信息区
text.value = '';
//创建一个新节点li
let co = document.createElement('p');
co.innerHTML = usert; //将用户输入的内容添加到新节点中
// let userleft = '<img src="inc/peter.jpg" width="30" style="border-radius:50%">'; // 设置用户头像
co.style.textAlign='left'
console.log(co)
// li.innerHTML = userPic + ' ' + userComment; // 将用户头像与用户的聊天内容合并
list.appendChild(co); //发送聊天内容,实际上就是将新节点插入到对话列表中
sum += 1; // 聊天记录自增1
//设置定时器,默认2秒后会自动回复
setTimeout(function(){
let info = [
'真烦人,有话快说,别耽误我玩抖音',
'除了退货,退款,咱们什么都可以聊',
'说啥,本姑娘怎么听不懂呢?再说一遍',
'在我方便的时候再回复你吧~~',
'投诉我的人多了,你算老几~~~'
];
let temp = info[Math.floor(Math.random()*4)];
let reply = document.createElement('p');
reply.innerHTML=temp
reply.style.textAlign='right'
reply.style.color='red'
list.appendChild(reply);
sum += 1;
},2000);
// 当聊天记录达到10条时清空窗口
if (sum > 10) {
list.innerHTML = '';
sum = 0;
}
}
</script>
</body>
</html>