abstract:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width
<!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>DOM: 模拟智能在线客服系统</title> <style> div:nth-child(1) { width: 450px; height: 650px; background: lightskyblue; margin: 30px auto; color: #333; box-shadow: 2px 2px 2px #808080; } h2 { text-align: center; margin-bottom: -10px; padding-top: 10px; } div:nth-child(2) { width: 400px; height: 500px; border: 4px double green; background: #efefef; margin: 20px auto 10px; } ul { list-style: none; line-height: 2em; overflow: hidden; padding: 15px; } table { width: 90%; height: 80px; margin: auto; } textarea { border: none; resize: none; background: lightyellow; } button { width: 60px; height: 40px; border: none; background: seagreen; color: white; margin-left: 5px; } button:hover { cursor: pointer; background: 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> //1. 获取相关元素 let btn = document.getElementsByTagName('button')[0]; let text = document.getElementsByName('text')[0]; let ul = document.getElementsByTagName('ul')[0]; let count = 0; //计数器 //添加点击事件,获取用户说的内容并发送到窗口 btn.onclick = function () { //获取用户提交的内容 if(text.value.length === 0){ alert('大官人,是不是忘了说点什么了'); return false; } let userComment = text.value; //将用户提交的内容获取并保存 text.value = ''; // 清空留言区 // 创建一个li let li = document.createElement('li'); li.innerHTML = userComment; //创建用户头像 let userPrc = '<img src="img/boy.jpeg" width="30" style="border-radius:50%;">'; li.innerHTML = userPrc + ' ' + userComment; ul.appendChild(li); //将用户信息添加到聊天窗口中 count += 1; //设置一个定时器 两秒以后回复 setTimeout(() => { //自动回复信息的模板 let info = [ '真烦人,有话快说,别耽误我玩抖音', '除了退货退款,什么都可以问', '说的啥,本姑娘怎么听不懂呢', '在我觉得方便的时候再回复你', '投诉我的人多了,你算老几' ] let temp = info[Math.floor(Math.random()*4)]; let reply = document.createElement('li'); let kefuPic = '<img src="img/fbb.jpg" width="30" style="border-radius:50%;">'; reply.innerHTML = kefuPic + ' ' + '<span style="color:red;">' + temp +'</span>'; ul.appendChild(reply); sum += 1; }, 2000); //清空窗口并将计数器清零 if(sum > 10){ ul.innerHTML = ''; sum = 0; } } </script> </body> </html>
Correcting teacher:查无此人Correction time:2019-05-05 10:19:28
Teacher's summary:完成的不错。等你学了php后,就可以把自动回复数据,让php返回。继续加油。