摘要:<!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: 模擬智能在線客服系統(tǒng)</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">發(fā)送</button></td> </tr> </table> </div> <script> //1. 獲取相關(guān)元素 let btn = document.getElementsByTagName('button')[0]; let text = document.getElementsByName('text')[0]; let ul = document.getElementsByTagName('ul')[0]; let count = 0; //計(jì)數(shù)器 //添加點(diǎn)擊事件,獲取用戶說的內(nèi)容并發(fā)送到窗口 btn.onclick = function () { //獲取用戶提交的內(nèi)容 if(text.value.length === 0){ alert('大官人,是不是忘了說點(diǎn)什么了'); return false; } let userComment = text.value; //將用戶提交的內(nèi)容獲取并保存 text.value = ''; // 清空留言區(qū) // 創(chuàng)建一個(gè)li let li = document.createElement('li'); li.innerHTML = userComment; //創(chuàng)建用戶頭像 let userPrc = '<img src="img/boy.jpeg" width="30" style="border-radius:50%;">'; li.innerHTML = userPrc + ' ' + userComment; ul.appendChild(li); //將用戶信息添加到聊天窗口中 count += 1; //設(shè)置一個(gè)定時(shí)器 兩秒以后回復(fù) setTimeout(() => { //自動(dòng)回復(fù)信息的模板 let info = [ '真煩人,有話快說,別耽誤我玩抖音', '除了退貨退款,什么都可以問', '說的啥,本姑娘怎么聽不懂呢', '在我覺得方便的時(shí)候再回復(fù)你', '投訴我的人多了,你算老幾' ] 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); //清空窗口并將計(jì)數(shù)器清零 if(sum > 10){ ul.innerHTML = ''; sum = 0; } } </script> </body> </html>
批改老師:查無此人批改時(shí)間:2019-05-05 10:19:28
老師總結(jié):完成的不錯(cuò)。等你學(xué)了php后,就可以把自動(dòng)回復(fù)數(shù)據(jù),讓php返回。繼續(xù)加油。