jQuery实现聊天对话框

效果图:左下角选择对话的角色,在对话框输入消息点击发送按钮,消息显示在上面界面当中,然后对话框内容被清空。

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style type="text/css"> .talk_con { width: 600px; height: 500px; border: 1px solid #666; margin: 50px auto 0; background: #f9f9f9; } .talk_show { width: 580px; height: 420px; border: 1px solid #666; background: #fff; margin: 10px auto 0; overflow: auto; } .talk_input { width: 580px; margin: 10px auto 0; } .whotalk { width: 80px; height: 30px; float: left; outline: none; } .talk_word { width: 420px; height: 26px; padding: 0px; float: left; margin-left: 10px; outline: none; text-indent: 10px; } .talk_sub { width: 56px; height: 30px; float: left; margin-left: 10px; } .atalk { margin: 10px; } .atalk span { display: inline-block; background: #0181cc; border-radius: 10px; color: #fff; padding: 5px 10px; } .btalk { margin: 10px; text-align: right; } .btalk span { display: inline-block; background: #ef8201; border-radius: 10px; color: #fff; padding: 5px 10px; } </style> <script src='https://www.jb51.net/js/jquery-1.12.4.js'></script> <script> $(function(){ $('#talksub').click(function(){ //发送单击,获取用户输入的数据value属性值 var vals=$('#talkwords').val() //如果输入的数据为空,则弹窗提示 if (vals=='') { alert('请输入数据!') return } //条件下拉列表中的value值是0还是1 var whovals=$('#who').val() var str="" //选择A发送还是B发送 if (whovals==0){ str='<div><span>A:'+ vals+'</span></div>' } else{ str='<div><span>B:'+ vals+'</span></div>' } //原有的内容+str,否则会覆盖掉原有内容 $('#words').html($('#words').html()+str) //发送完数据后清空输入框 $('#talkwords').val('') }) }) </script> </head> <body> <div> <div> <div><span>A:你还好吗?</span></div> <div><span>B:嗯,你呢?</span></div> </div> <div> <select> <option value="0">A:</option> <option value="1">B:</option> </select> <input type="text"> <input type="button" value="发送"> </div> </div> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/1a846d85fbdfde632b939136595dce0a.html