实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>自动应答</title> <style type="text/css"> div:nth-child(1){ width: 800px; height: 450px; background-color: lightblue; margin: 0 auto; color: #333; box-shadow: 2px 2px 2px #808080; border: 1px solid red; overflow: hidden; } h2{ text-align: left; margin-top: 0; } div:nth-child(2){ width:780px; height:300px; border: 1px solid green; background-color: #eff0f2; margin: auto; } ul{ list-style-type: none; line-height: 2em; overflow: hidden; padding: 15px; } table{ width:700px; height: 80px; margin: auto; } textarea{ width: 740px; height: 100px; margin-left: 0px; padding: 0; resize:none; border: none; background-color: yellow; float: left; } button{ width:50px; float: left; } /* .div .answer{ float: right; }*/ </style> </head> <body> <div> <h2>JD.COM</h2> <div contenteditable="true"> <ul> <li></li> <li></li> </ul> </div> <table> <tr> <td><textarea name="text" id="" cols="30" rows="4"></textarea></td> <td><button type=button>发送</button></td> <!-- <td><button>关闭</button></td> --> </tr> </table> </div> <script type="text/javascript"> var btn = document.getElementsByTagName('button')[0] var text = document.getElementsByName('text')[0] var list = document.getElementsByTagName('ul')[0] var sum = 0 btn.onclick = function () { if (text.value.length == 0) { alert('空') return false } var userComment = text.value text.value = '' var li = document.createElement('li') var now =new Date().toLocaleTimeString(); li.innerHTML = '<span style="color:blue">'+"我 "+now+":"+"<br/>"+userComment+'</span>' list.appendChild(li) sum += 1 setTimeout(function(){ var info = ['1','2','3','4','5'] var temp = info[Math.floor(Math.random()*3)] var reply = document.createElement('li') reply.innerHTML ='<span style="color:red">'+"JD.COM "+now+":"+"<br/>"+temp+'</span>' list.appendChild(reply) sum += 1 },1000) if (sum >3 ) { list.innerHTML = '' sum = 0 } } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例