摘要:通过在线客服系统的练习,对DOM元素操作节点元素有了进一步的认识,经过实战,对操作元素的方法有了进一步的锻炼。<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>在线客服系统</title> <style 
通过在线客服系统的练习,对DOM元素操作节点元素有了进一步的认识,经过实战,对操作元素的方法有了进一步的锻炼。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>在线客服系统</title> <style type="text/css"> div:nth-child(1) { width: 450px; height: 650px; background-color: #ccc; 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-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: white; overflow-y: auto; } button { width: 60px; height: 40px; background-color: blue; color: white; border: none; } button:hover { cursor: pointer; background-color: pink; } </style> </head> <body> <div> <h2>在线客服聊天</h2> <div> <ul> <li></li> </ul> </div> <table> <tr> <td><textarea cols="50" rows="4" id="txtInput" name="text"></textarea></td> <td><button type=button>发送</button></td> </tr> </table> </div> <script type="text/javascript"> //获取标签 let btn = document.getElementsByTagName('button').item(0); let content = document.getElementById('txtInput'); let list = document.getElementsByTagName('ul').item(0); let count=0; btn.onclick = function () { if (content.value.length === 0) { alert('请输入聊天内容。'); return false; } let contentValue = content.value; //清空聊天框 content.value = ''; //创建一个新节点li let li = document.createElement('li'); li.innerHTML = contentValue; li.innerHTML = contentValue; list.appendChild(li); //添加聊天内容 count++;//累计点击次数 //设置定时器,默认1秒后会自动回复 setTimeout(function(){ let res = [ '您好,很高兴为您服务', '正在查询,请稍后', '可以提供联系电话吗?' ]; let temp = res[Math.floor(Math.random()*3)]; let reply = document.createElement('li'); reply.innerHTML = '<span style="color:red">'+temp+'</span>'; list.appendChild(reply); count++;//累计点击次数 },1000); //超出10条则清空内容 if (count > 10) { list.innerHTML = ''; count = 0; } } </script> </body> </html>
批改老师:灭绝师太批改时间:2018-11-08 17:40:48
老师总结:还是比较实用的,可以试着自己模拟一个小案例,把所学到的知识都运用进去