模拟智能在线客服系统
文昌
2019-02-20 17:52:02
265
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DOM实战:模拟只能在线客服系统</title>
<style>
div:nth-child(1) {
width: 450px;
height: 650px;
background: lightskyblue;
margin: 30px auto;
color: #333333;
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: #efefef;
margin: 20px auto 10px;
}
ul {
list-style-type: none;
line-height: 2em;
overflow: hidden;
padding: 15px;
}
table {
width: 90%;
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>
<ul>
<li></li>
</ul>
</div>
<table>
<tr>
<td align="right"><textarea name="text" cols="50" rows="4"></textarea></td>
<td align="left"><button type="button">发送</button></td>
</tr>
</table>
</div>
<script>
//获取页面中的相关元素
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');
let userPic = '<img src="inc/peter.jpg" width="30px" style="border-radius: 50%">';
li.innerHTML = userPic +' ' + userComment;
list.appendChild(li);
sum += 1;
setTimeout(function () {
let info = [
'您好,有什么能为您服务的吗?',
'客服比较忙,稍后回复您',
];
let temp = info[Math.floor(Math.random()*3)];
let reply = document.createElement('li');
let kefuPic = '<img src="inc/zly.jpg" width="30px" style="border-radius: 50%">';
reply.innerHTML = kefuPic + ' ' + '<span style="color:red">' + temp + '</span>';
list.appendChild(reply);
sum += 1;
},2000);
if (sum > 10) {
list.innerHTML = '';
sum = 0;
}
}
</script>
</body>
</html>