abstract:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>智能客服</title>  
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>智能客服</title> <style type="text/css"> .bg { width: 450px; height: 650px; background-color: lightskyblue; margin: 10px auto; color: #333; box-shadow: 2px 2px 2px #808080 } h2 { text-align: center; margin-bottom: -10px; } .ft{ width: 400px; height: 500px; border: 4px double green; background-color: #efefef; margin: auto; margin-top: 20px; } 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: lightpink; } button { width: 60px; height: 40px; background-color: seagreen; color: white; border: none; /*text-align: left;*/ } button:hover { cursor: pointer; background-color: lightyellow; } </style> </head> <body> <h2>在線客服</h2> <div> <div> <ul> <li></li> </ul> </div> <table> <tr> <td><textarea name="text" id="" cols="50" rows="8"></textarea></td> <td><button type="button" style="color: #000;">發(fā)送</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; //添加按鈕點(diǎn)擊事件,獲取用戶(hù)數(shù)據(jù)并推送到對(duì)話窗口中 btn.onclick = function () { if (text.value.length === 0) { alert('輸入為空,說(shuō)點(diǎn)什么再點(diǎn)吧'); return false; } let userComment = text.value; //立即清空用戶(hù)信息區(qū) // text.value = ''; //創(chuàng)建一個(gè)新節(jié)點(diǎn)li let li = document.createElement('li'); li.innerHTML = userComment; //將用戶(hù)輸入的內(nèi)容添加到新節(jié)點(diǎn)中 let userPic = '<img src="inc/peter.jpg" width="30" style="border-radius:50%">'; // 設(shè)置用戶(hù)頭像 li.innerHTML = userPic + ' ' + userComment; // 將用戶(hù)頭像與用戶(hù)的聊天內(nèi)容合并 list.appendChild(li); //發(fā)送聊天內(nèi)容,實(shí)際上就是將新節(jié)點(diǎn)插入到對(duì)話列表中 sum += 1; // 聊天記錄自增1 //設(shè)置定時(shí)器,默認(rèn)2秒后會(huì)自動(dòng)回復(fù) setTimeout(function(){ let info = [ '加微信給哥哥發(fā)照片哦', '加個(gè)QQ嗎', '在的呢', '怎么了嘛', '人家沒(méi)空啦', ]; let temp = info[Math.floor(Math.random()*5)]; //取1-5之間的一個(gè)整數(shù):Math.floor(Math.random()*6 + 1) let reply = document.createElement('li'); let kefuPic = '<img src="inc/zly.jpg" width="30" style="border-radius:50%;">'; reply.innerHTML = kefuPic + ' ' + '<span style="color:orange">'+temp+'</span>'; list.appendChild(reply); sum += 1; },2000); // 當(dāng)聊天記錄達(dá)到10條時(shí)清空窗口 if (sum > 11) { list.innerHTML = ''; sum = 0; } } </script> </body> </html>
變量調(diào)用和字符串拼接比較懵
Correcting teacher:韋小寶Correction time:2018-11-23 17:48:02
Teacher's summary:嗯!寫(xiě)的很不錯(cuò)!前端就得多找這種案例來(lái)進(jìn)行練習(xí)哦!繼續(xù)加油吧!