Correction status:qualified
Teacher's comments:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>3.实战: 仿机器人聊天窗口</title> <style type="text/css"> div:nth-child(1) { width: 450px; height: 650px; background-color: lightskyblue; margin: 30px auto; color: #333; box-shadow: 4px 3px 4px #808080 } h2 { text-align: center; margin-bottom: -10px; } div:nth-child(2) { width: 400px; height: 500px; border: 4px double green; background-color: #efefef; /* background:black; */ margin: 20px auto 10px; } ul { list-style: none; line-height: 1em; /*border: 1px solid red;*/ overflow: hidden; padding: 15px; } table { width: 90%; height:80px; margin: auto; } textarea{ /*width: 300px;*/ border: none; resize: none; background-color: lightyellow; } button { width: 60px; height: 40px; background-color: seagreen; color: white; border: none; /*text-align: left;*/ } button:hover { cursor: pointer; background-color: orange; } </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>发送</button></td> </tr> </table> </div> <script type="text/javascript"> //获取到页面中的按钮,文本域,对话内容区 var btn = document.getElementsByTagName('button')[0] var text = document.getElementsByTagName('textarea')[0] // var text = document.getElementsByName('text')[0] var list = document.getElementsByTagName('ul')[0] var sum = 0 //添加按钮点击事件,获取用户数据并推送到对话窗口中 btn.onclick = function () { // alert(text.value) //获取用户提交的内容 if (text.value.length == 0) { alert('客官:是不是忘记输入内容了~~') return false } var userComment = text.value //立即清空用户信息区 text.value = '' //创建一个新节点li var li = document.createElement('li') li.innerHTML = userComment var userPic = '<img src="../images/peter.jpg" width="30" style="border-radius:50%">' li.innerHTML = userPic+userComment //将新节点插入到对话列表中 list.appendChild(li) sum += 1 setTimeout(function(){ var info = ['你好烦人呀,本姑娘好累,不知道怜香惜玉吗?','除了退货,退款,维修,什么问题都可以问','啥事呀,我的帅哥哥'] var temp = info[Math.floor(Math.random()*3)] //取1-5之间的一个整数:Math.floor(Math.random()*6 + 1) var reply = document.createElement('li') var kefuPic = '<img src="../images/zly.jpg" width="30" style="border-radius:50%;">' // reply.innerHTML = '你有啥事呀,我的帅哥哥' +kefuPic reply.innerHTML = kefuPic + '<span style="color:red">'+temp+'</span>' // reply.style.float = 'right' list.appendChild(reply) sum += 1 },1000) if (sum > 10) { list.innerHTML = '' sum = 0 } } </script> </body> </html>
点击 "运行实例" 按钮查看在线实例