<!DOCTYPE html>
<style>
div:nth-child(1){
width: 450px;
height: 650px;
background-color: lightskyblue;
margin: 30px auto;
color: #333;
box-shadow: 2px 2px 2px #888888;
}
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;
overflow: hidden;
padding: 15px;
}
table{
width: 90%;
height: 80px;
margin: auto;
}
textarea{
resize:none;
border: none;
background-color: lightyellow;
}
button{
width: 60px;
height: 40px;
background-color: seagreen;
color: white;
border: none;
}
button:hover{
cursor: pointer;
background-color: orange;
}
</style>
<html>
<head>
<meta charset="utf-8">
<title>dom实战:模拟智能在线客服系统</title>
</head>
<body>
<div>
<h2>在线客服</h2>
<div>
<ul>
</ul>
</div>
<table>
<tr>
<td><textarea name="text" id="" cols="50" rows="4"></textarea></td>
<td><button type="button">发送</button></td>
</tr>
</table>
</div>
</body>
</html>
<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 userComent = text.value;//将用户提交的内容获取并保存
text.value = '';
let li = document.createElement('li');
let userPic = '<img src="1.jpg" alt="" style="border-radius:50%;" width="30"; height="30";>';
li.innerHTML = userPic + ' ' + userComent;
list.appendChild(li);
setTimeout(function(){
let info = [
'你好,有什么问题吗?',
'请稍等',
'你好'
];
let temp = info[Math.floor(Math.random()*3)];
let li = document.createElement('li');
let keyPic = '<img src="1.jpg" alt="" style="border-radius:50%;" width="30"; height="30";>';
li.innerHTML = keyPic + ' ' + '<span style="color:red">'+temp+'</span>';
list.appendChild(li);
sum+=2;
}, 2000);
if(sum>4){
let first = list.firstElementChild;
list.removeChild(first);
console.log(first);
}
if(sum>4){
let first = list.firstElementChild;
list.removeChild(first);
console.log(first);
}
console.log(sum);
}
</script>