摘要:<!DOCTYPE html> <html> <head> <title>模擬智能在線(xiàn)客服系統(tǒng)</title> <style type="text/css"> .div_1{ width: 45
<!DOCTYPE html> <html> <head> <title>模擬智能在線(xiàn)客服系統(tǒng)</title> <style type="text/css"> .div_1{ width: 450px; height: 650px; background-color: lightskyblue; margin: 30px auto; color: #333; box-shadow: 2px 2px 2px #808080; } h2{ text-align: center; margin-bottom:-10px; } .div_2{ width: 400px; height: 500px; border: 4px double green; background-color: #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: 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 class="div_1"> <h2>在線(xiàn)客服</h2> <div class="div_2"> <ul> <li></li> </ul> </div> <table> <tr> <td class="right"><textarea name="text" cols='50' rows='4'></textarea></td> <td class="left"><button type="button">發(fā)送</button></td> </tr> </table> </div> <script type="text/javascript"> //獲取到頁(yè)面上相關(guān)的元素 var btn=document.getElementsByTagName('button')[0]; var text=document.getElementsByName('text')[0]; var list =document.getElementsByTagName('ul')[0]; var sum = 0; //這是計(jì)數(shù)器 //添加點(diǎn)擊事件,獲取用戶(hù)說(shuō)的內(nèi)容并發(fā)生到窗口 btn.onclick=function(){ //獲取用戶(hù)提交的內(nèi)容 if(text.value.length===0){ alert('是不是忘記填寫(xiě)信息?'); return flase; } var userComment = text.value;//將用戶(hù)提交的信息獲取并保存 text.value=''; //創(chuàng)建一個(gè)li var li = document.createElement('li'); //創(chuàng)建個(gè)用戶(hù)頭像 var userPic='<img src="1.jpg" width="30" style="border-radius:50%">'; li.innerHTML=userPic+' '+userComment; list.appendChild(li); //將用戶(hù)信息添加到窗口中 sum+=1; //設(shè)置一個(gè)定時(shí)器 setTimeout(function(){ var info=[ '你真了不起哦', '你是我的小天使', '你個(gè)大還丹', '今天下雪了,天氣好冷', '你是逗逼嗎?' ]; var temp = info[Math.floor(Math.random()*4)]; var replay = document.createElement('li'); var kefuPic = '<img src="2.jpg" width="30" style="border-radius:50%">'; replay.innerHTML=kefuPic + ' ' + '<span style="color:red;">'+temp+'</span>'; list.appendChild(replay); sum+=1; },2000); //清空窗口并將計(jì)時(shí)器清零 if(sum>10){ list.innerHTML=''; sum=0; } } </script> </body> </html>
本節(jié)課程主要學(xué)習(xí)到了獲取元素,關(guān)聯(lián)元素,插入元素,清空窗口和計(jì)數(shù)器和setTimeout定時(shí)器的設(shè)置
批改老師:韋小寶批改時(shí)間:2018-12-09 09:02:36
老師總結(jié):和上一篇寫(xiě)的一樣的很不錯(cuò)!課后要記得多練習(xí)哦!