サマリー:基本思路:1.首先設置點擊事件,通過if條件判斷語句判斷文本框內(nèi)是否輸入文本,未輸入文字時會有彈窗提示。if (text.value.length === 0) { alert('您好,請輸入內(nèi)容哦~'); return false; &
基本思路:
1.首先設置點擊事件,通過if條件判斷語句判斷文本框內(nèi)是否輸入文本,未輸入文字時會有彈窗提示。
if (text.value.length === 0) { alert('您好,請輸入內(nèi)容哦~'); return false; }
2.將文本框儲存在變量中,創(chuàng)建一個新的li標簽,以顯示輸入文本的內(nèi)容,將此內(nèi)容和用戶頭像連接起來,使其顯示與一行內(nèi)。
let userComment = text.value; let li = document.createElement('li'); li.innerHTML = userComment; let userPic = '<img src="inc/boy.jpeg" width="30" style="border-radius:50%">'; li.innerHTML = userPic + ' ' + userComment; list.appendChild(li); sum += 1;
3.創(chuàng)立客服在線回復的部分,首先創(chuàng)立一個數(shù)組,內(nèi)容是客服回答的一些語句,將其存放在變量中,通過創(chuàng)立隨機數(shù)的方式在此數(shù)組隨機獲得相關的語句。將隨機獲得的一段字符串語句存放于相應的變量之中,使其能和一個有客服頭像的語句存在于一行,作為客服的智能回復。
setTimeout(function(){ let info = [ '請問您還想了解什么嗎?我們很樂意為您提供!', '請不要著急,我已安排了,很快就給您辦好。', '請稍等,我查一下再答復您。', '對不起,我們一定會努力改進的!', '您好,請問還有什么需求嗎?', '對不起,我再幫您想別的辦法。', '這是我們應該做的!' ]; let temp = info[Math.floor(Math.random()*6+1)]; let reply = document.createElement('li'); let kefuPic = '<img src="inc/gyy.jpg" width="30" style="border-radius:50%;">'; reply.innerHTML = kefuPic + ' ' + '<span style="color:red">'+temp+'</span>'; list.appendChild(reply); sum += 1; },500);
將客服回答的部分放于一個計數(shù)器函數(shù)中,讓客服在每0.5秒后能智能回復。將自己每次輸入的內(nèi)容和客服回答的內(nèi)容看成一次回答,完成后添加到計數(shù)器中,通過if條件判斷語句判斷,如果計數(shù)器大于10,則自動刷新頁面。
if (sum > 10) { list.innerHTML = ''; sum = 0; }
注意事項:
在數(shù)組中獲得相關的字符部分時,這部分可以在后期學習了數(shù)據(jù)庫后再在數(shù)據(jù)庫中調(diào)取相關數(shù)據(jù);
獲取隨機數(shù)時,通過使用Math.floor(Math.random()*(B-A)+A)的方法可以獲取A到B之間的隨機數(shù);
在敲代碼時需要特別細心,像一個‘.’沒打這樣的問題可能就會使整個功能無法實現(xiàn)。
完整代碼:
<!DOCTYPE html> <html> <head> <title>模擬智能在線客服系統(tǒng)</title> <meta charset="utf-8"> <style type="text/css"> *{margin: 0px;padding: 0px} div:nth-child(1) { width: 450px; height: 650px; background-color: #acf; margin: 30px auto; color: #333 box-shadow: 2px 2px 2px #808080; } h2{ text-align: center; margin-bottom: -10px; font-weight: 400; font-family: 華文琥珀; } div:nth-child(2) { width: 400px; height: 500px; border: 5px double #ff6700; background-color: #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-color: lightyellow; } button { width: 60px; height: 40px; background-color: #F1AD7C; color: #000; border: none; font-family: 幼圓; font-weight: bold; } button:hover { cursor: pointer; background-color: ; color: #fff; } </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"></textarea></td> <td align="left"><button type="button">發(fā)送</button></td> </tr> </table> </div> <script type="text/javascript"> let btn = document.getElementsByTagName('button')[0]; let text = document.getElementsByName('text')[0]; let list = document.getElementsByTagName('ul')[0]; let sum = 0; btn.onclick = function () { if (text.value.length === 0) { alert('您好,請輸入內(nèi)容哦~'); return false; } let userComment = text.value; text.value = ''; let li = document.createElement('li'); li.innerHTML = userComment; let userPic = '<img src="inc/boy.jpeg" width="30" style="border-radius:50%">'; li.innerHTML = userPic + ' ' + userComment; list.appendChild(li); sum += 1; setTimeout(function(){ let info = [ '請問您還想了解什么嗎?我們很樂意為您提供!', '請不要著急,我已安排了,很快就給您辦好。', '請稍等,我查一下再答復您。', '對不起,我們一定會努力改進的!', '您好,請問還有什么需求嗎?', '對不起,我再幫您想別的辦法。', '這是我們應該做的!' ]; let temp = info[Math.floor(Math.random()*6+1)]; let reply = document.createElement('li'); let kefuPic = '<img src="inc/gyy.jpg" width="30" style="border-radius:50%;">'; reply.innerHTML = kefuPic + ' ' + '<span style="color:red">'+temp+'</span>'; list.appendChild(reply); sum += 1; },500); if (sum > 10) { list.innerHTML = ''; sum = 0; } } </script> </body> </html>
添削の先生:查無此人添削時間:2019-03-18 09:47:59
先生のまとめ:完成的不錯,思路很清晰。 數(shù)據(jù)之后可以用php請求數(shù)據(jù)庫,有更多的語句進行自動回復。