编写一个在线客服功能(仿机器人聊天窗口)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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" onkeydown="addComment(this)" autofocus></textarea></td>
<!--<td align="right"><input type="text" name="text" onkeydown="addComment(this)" autofocus></td>-->
<td align="left"><button type=button>发送</button></td>
</tr>
</table>
</div>
<script type="text/javascript">
//获取到页面中的按钮,文本域,对话内容区
var btn = document.getElementsByTagName('button')[0];
var text = document.getElementsByName('text')[0];
var list = document.getElementsByTagName('ul')[0];
var sum = 0;
//添加按钮点击事件,获取用户数据并推送到对话窗口中
btn.onclick=function talk(){
// console.log(text.value);
//获取用户提交的内容
if(text.value.length===0){
alert('好歹说点啥嘛!!!');
return false;
}
var userComment=text.value;
//立即清空用户信息区
text.value='';
//创建一个新节点li
var li=document.createElement('li');
var userPic='<img src="images/user.jpg" width="30" style="border-radius: 50%">';
li.innerHTML=userPic+'用户:'+'<br>'+'<span style="color:red">'+userComment+'</span>'+' ';
//将新节点插入到对话列表中
list.appendChild(li);
sum+=1;
//设置用户输入的信息右侧显示
li.style.textAlign="right";
// 定时器,2秒后执行
setTimeout(function(){
var info=['来啦,老弟!','来唠嗑吧!!!','没事来看看我!','好开心!','人家不高兴了啦!'];
var temp=info[Math.floor(Math.random()*5)];//获取随机的内容
var reply = document.createElement('li'); /*创建新元素用来保存回复内容*/
var kefupic='<img src="images/kf.jpg" width="30" style="border-radius:50%;">';
reply.innerHTML=kefupic+'客服:'+'<br>'+' '+'<span style="color:orchid">'+temp+'</span>';
list.appendChild(reply);//将回复内容添加到窗口中
sum+=1;
//设置回复信息左侧显示
reply.style.textAlign="left";
},2000);
//如果回复内容大于6条则清空聊天窗口
if(sum>6){
list.innerHTML='';//清空对话窗口内容
sum=0;//清空计数器
}
}
//回车键发送信息
function addComment(comment){
if(event.keyCode ===13){
// console.log('aaa');
btn.onclick();
}
}
</script>
</body>
</html>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号