摘要:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>DOM實(shí)戰(zhàn):模擬智能在線客服系統(tǒng)</title> <style type="text/css"> &n
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>DOM實(shí)戰(zhàn):模擬智能在線客服系統(tǒng)</title> <style type="text/css"> div:nth-child(1){ width: 450px; height: 650px; background-color: lightskyblue; color: #333; box-shadow: 2px 2px 2px $808080; } h2 { text-align: center; margin-bottom: -10px; } div:nth-child(2){ width: 400px; height: 500px; border: 2px solid #ccc; background: #efefef; margin: 20px auto 10px; } ul { list-style-type:none; line-height: 2em; overflow:hidden; padding: 15px; } table{ width: 280px; height: 80px; margin: auto; } textarea{ border:none; resize:none; background-color: lightyellow; } button{ width: 60px; height: 40px; background-color: seagreen; color:white; border: none; } button:hover{ cursor: pointer; background-color:orange; } </style> </head> <body> <div> <h2>在線客服</h2> <div> <ul> <li></li> </ul> </div> <table> <tr> <td><textarea name="text" cols="50" rows="4"></textarea></td> <td><button type="button">發(fā)送</button></td> </tr> </table> </div> </body> <script type="text/javascript"> //獲取到頁(yè)面中的相關(guān)元素 let btn = document.getElementsByTagName('button')[0]; let text = document.getElementsByName('text')[0]; let list = document.getElementsByTagName('ul')[0]; var sum = 0; //計(jì)數(shù)器 //添加點(diǎn)擊事件,獲取用戶說(shuō)的內(nèi)容兵發(fā)送到窗口 btn.onclick = function(){ //獲取用戶提交的內(nèi)容 if(text.value.length === 0){ alert('你沒(méi)有輸入信息,請(qǐng)輸入'); return false; } let userComment = text.value; //將用戶提交的內(nèi)容獲取并保存 text.value = ''; //立即將用戶留言區(qū)清空 // 創(chuàng)建一個(gè)li let li = document.createElement('li'); li.innerHTML = userComment; //文本框的內(nèi)容 // 用戶頭像 let userPic = '<img src="inc/touxiang1.jpg" width="32"; style="border-radius:50%">'; li.innerHTML = userPic + '' + userComment; list.appendChild(li); //將用戶信息添加到聊天窗口中 sum += 1; //設(shè)置定時(shí)器,2秒自動(dòng)回復(fù) setTimeout(function(){ //自動(dòng)回復(fù)信息的模版 let info = [ '您好,有什么可以幫助你?', '除了退貨,退款,咱們什么都可以聊', '說(shuō)啥,本姑娘怎么聽(tīng)不懂呢?再說(shuō)一遍', '在我方便的時(shí)候再回復(fù)你吧~~', '沒(méi)有事情,就關(guān)閉會(huì)話吧', ]; let temp = info[Math.floor(Math.random()*4)]; let reply = document.createElement('li'); let kefuPic = '<img src="inc/touxiang2.jpg" width="32"; style="border-radius:50%">'; reply.innerHTML =kefuPic + '' +'<span style="color:red;float:right;">'+temp+'</span>'; list.appendChild(reply); sum += 1; },2000); //清空窗口并將計(jì)數(shù)器清零 if(sum > 10){ list.innerHTML = ''; sum = 0; } } </script> </html>
總結(jié):
1、用value.length === 0 前面點(diǎn)上變量,用if來(lái)判斷是否有輸入內(nèi)容;
2、使用text.value;將數(shù)據(jù)保存,然后用text.value = ''; 清空留言區(qū)
3、let userPic = '<img src="inc/touxiang1.jpg" width="32"; style="border-radius:50%">'; 用html直接引入頭像,直接定義變量,然后使用appendChild 添加信息
4、定時(shí)器setTimeout(function(){
},1500); 的用法
5、info.[Math.floor(Math.random()*4)]; 隨機(jī)獲取info變量里的數(shù)組中其中1條。
批改老師:天蓬老師批改時(shí)間:2019-01-01 09:13:28
老師總結(jié):document.getElementsByTagName('button')[0];推薦改成: document.getElementsByTagName('button').item(0), 這樣是不是更好看些呢?