abstrak:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>DOM實戰(zhàn):模擬在線客服系統(tǒng)</title> <style> div:nth-child(1){width
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>DOM實戰(zhàn):模擬在線客服系統(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;} div:nth-child(2){width: 400px;height: 500px;border: 4px double green; background: #efefef;margin: 20px auto 10px; } ul{list-style-type: none;line-height: 2em;overflow: hidden;padding: 15px;} table{width: 90%;height: 80px;margin: auto;} textarea{border:0;resize: none;background: lightyellow;} button{width: 60px;height: 40px;background: seagreen;color: white;border:0;} button:hover{cursor: pointer;background: orange;} li span{background: #ff6800;} </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 align="left"><button type="button">發(fā)送</button></td> </tr> </table> </div> </body> </html> <script> // 獲取到頁面中相關元素 let btn = document.getElementsByTagName('button')[0]; let text = document.getElementsByName('text')[0]; let ul = document.getElementsByTagName('ul')[0]; let sum = 0; // 計數(shù)器初始化 // 添加點擊事件,獲取用戶信息并發(fā)送到窗口 btn.onclick = function(){ let msg = text.value; // 獲取用戶提交的信息內(nèi)容并保存到變量中 text.value = ''; // 創(chuàng)建一個li元素存放發(fā)送的信息 let li = document.createElement('li'); // 用戶頭像 let Img = '<img src="images/2.jpg" width="30" style="border-radius:50%;margin-left:0px;">'; li.innerHTML = '<span style="background:lightgreen;margin-left:70%;"> '+msg+'</span>'+Img; ul.appendChild(li); sum += 1; // 自動回復信息 setTimeout(function(){ let info = [ '200賣不賣', '怎么沒反應了', '你還在嗎?', '價錢不好可以商量啊!', '這樣子恐怕不好吧!', '我先轉(zhuǎn)賬給你,' ]; let tmp = info[Math.floor(Math.random()*5)]; let server = document.createElement('li'); let img = '<img src="images/1.jpg" width="30" style="border-radius:50%;">'; server.innerHTML = img+'<span style="background:#ccc;"> '+tmp+'</span>'; ul.appendChild(server); sum += 1; },2000); } </script>
Guru membetulkan:韋小寶Masa pembetulan:2019-03-04 09:16:18
Rumusan guru:寫的非常不錯 其實這個還可以實現(xiàn)的更為智能一點 不過這個自動回復已經(jīng)很不錯了