jquery添加div实现消息聊天框


Posted in jQuery onFebruary 08, 2020

本文实例为大家分享了jquery添加div实现消息聊天框的具体代码,供大家参考,具体内容如下

上代码

<html>
<head>
<style>
* {
  margin: 0;
  padding: 0;
}

.border {
  margin-left: 300px;
  width: 900px;
  background-color: white;
  position: relative;
  border: 1px solid rgb(221, 221, 221);
}

.border .border-next {
  background-color: #dcad50;
  position: relative;
  height: 23px;
  line-height: 40px;
  display: flex;
  padding: 10px 60px 10px 80px;
}

.border-next .people {
  background-color: #dcad50;
  font-size: 20px;
  color: black;
  font-family: 楷体;
  margin-left: 300px;
}

.border .border-second {
  background-color: white;
  margin-left: 0px;
  width: 700px;
  height: 530px;
  flex: 1;
  flex-direction: column;
  overflow-y: auto;
  border-right: 1px solid rgb(221, 221, 221);
  border-bottom: 1px solid rgb(221, 221, 221);
}

.border .border-img {
  background-color: white;
  margin-left: 0px;
  width: 700px;
  height: 30px;
  border-right: 1px solid rgb(221, 221, 221);
  border-bottom: 1px solid rgb(221, 221, 221);
}

.border-bottom {
  display: flex;
  width: 700px;
  height: 120px;
  background-color: white;
  overflow: auto;
  font-size: 20px;
  border-style: solid;
  border-color: #FFFFFF;
  border-right: 1px solid rgb(221, 221, 221);
}

.button {
  display: flex;
  margin-left: 530px;
}

.button .shut {
  background-color: white;
  width: 70px;
  height: 30px;
  font-size: 20px;
  text-align: center;
  border: 1px solid rgb(221, 221, 221);
}

.button .send {
  background-color: white;
  margin-left: 15px;
  width: 70px;
  height: 30px;
  font-size: 20px;
  text-align: center;
  border: 1px solid rgb(221, 221, 221);
  background-color: #DBAC50;
}

.replyChat {
  display:flex;
  width: 150px;
  background: #12B7F5;
  border-radius: 5px;
  /* 圆角 */
  position: relative;
  margin-left: 500px;
  align-content: center;
  margin-bottom: 30px;
}

.sendChat {
  display:flex;
  width: 150px;
  background: #E5E5E5;
  border-radius: 5px;
  /* 圆角 */
  position: relative;
  margin-left: 50px;
  align-content: center;
  margin-bottom: 30px;
  border-color: white white white #E5E5E5;
}

.sendChat span {
  display: inline-block;
  margin-left: 10px;
  line-height: 35px;
}

.replyChat span {
  display: inline-block;
  margin-left: 10px;
  line-height: 35px;
}

.sendChat .arrows {
  position: absolute;
  top: 5px;
  left: -16px;
  /* 圆角的位置需要细心调试哦 */
  width: 0;
  height: 0;
  font-size: 0;
  border: solid 8px;
  border-color: white #E5E5E5 white white;
}

.replyChat .arrow {
  position: absolute;
  top: 5px;
  right: -16px;
  /* 圆角的位置需要细心调试哦 */
  width: 0;
  height: 0;
  font-size: 0;
  border: solid 8px;
  border-color: white white white #12B7F5;
}

.chatTouXiang {
  width: 50px;
  height: 50px;
  border-radius: 50%; 
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  background-image: url(img/tou.png);
}
.chatCnt{

}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>聊天助手</title>
<script src="js/jquery-1.8.3.min.js"></script>
<script>
  window.οnlοad=function(){
    $(".arrow").hide();
    $(".arrows").hide();
  }
  document.onkeydown = function(e) {
    if (e.keyCode == 13 && e.ctrlKey) {
      // 这里实现换行
      document.getElementById("sendContent").value += "\n";
    } else if (e.keyCode == 13) {
      // 避免回车键换行
      e.preventDefault();
      // 下面写你的发送消息的代码
      f();
    }
  }
  function f() {
    var cnt = $("#sendContent").val();
    if(cnt == '')alert('内容不能为空');  
    if(cnt != ''){
      var node = document.createElement('div');
      node.className = 'sendChat';
      var span = document.createElement('span');
      span.innerHTML = cnt;
      var arrow = document.createElement('div');
      arrow.className = 'arrows';
      node.appendChild(span);
      node.appendChild(arrow);
      $(".border-second").append($(node));
      $("#sendContent").val('');
      $.ajax({
        data : cnt,
        type : "post",
        url : "CharServlet?id=" + cnt,
        dataType : "json",
        success : function(msg) {
          var node = document.createElement('div');
          node.className = 'replyChat';
          var span = document.createElement('span');
          span.innerHTML = msg.text;
          var arrow = document.createElement('div');
          arrow.className = 'arrow';
          node.appendChild(arrow);
          node.appendChild(span);

          $(".border-second").append($(node));

          var boderSecondDiv = $('.border-second');
          var lastChild = boderSecondDiv[0].lastChild;
          var lastChildH = lastChild.offsetTop;
          var h = 0;
          for (var i = 0, len = lastChild.children.length; i < len; i++) {

            h += lastChild.children[i].offsetHeight;
          }
          boderSecondDiv[0].scrollTop = lastChildH + h;

        },
        error : function(msg) {
          alert("请求失败");
        }
      });
    }
  }
</script>
</head>

<div class="frame">
  <div class="border">
    <div class="border-next">
      <div class="people">聊天助手</div>
    </div>
    <div class="border-second">
      <div class="chatCnt">
      <div class="chatTouXiang"></div> 
      <div class="sendChat">
        <span></span>
        <div class="arrows"></div>
      </div>
      </div>
      <div class="replyChat">
        <span></span>
        <div class="arrow"></div>
      </div>
      <br>
    </div>
    <div class="border-img"></div>
    <textarea id="sendContent" class="border-bottom"></textarea>
    <div class="button">
      <button class="shut">关闭</button>
      <button id="selectBtn" class="send" onclick="f()">发送</button>
    </div>

  </div>
</div>
</body>
</html>

jquery添加div实现消息聊天框

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

jQuery 相关文章推荐
jquery实现图片放大点击切换
Jun 06 jQuery
jQuery Ajax使用FormData上传文件和其他数据后端web.py获取
Jun 11 jQuery
深入理解jquery的$.extend()、$.fn和$.fn.extend()
Jul 08 jQuery
jquery动态赋值id与动态取id方法示例
Aug 21 jQuery
jQuery实现简单的回到顶部totop功能示例
Oct 16 jQuery
jQuery实现的鼠标响应缓冲动画效果示例
Feb 13 jQuery
jQuery插件Validation表单验证详解
May 26 jQuery
jQuery md5加密插件jQuery.md5.js用法示例
Aug 24 jQuery
jquery实现选项卡切换代码实例
May 14 jQuery
jquery实现聊天机器人
Feb 08 jQuery
jQuery事件模型默认行为执行顺序及trigger()与 triggerHandler()比较实例分析
Apr 30 jQuery
jquery更改元素属性attr()方法操作示例
May 22 jQuery
jQuery实现聊天对话框
Feb 08 #jQuery
jquery实现聊天机器人
Feb 08 #jQuery
jQuery实现获取多选框的值示例
Feb 07 #jQuery
jQuery操作选中select下拉框的值代码实例
Feb 07 #jQuery
如何使用Jquery动态生成二级选项列表
Feb 06 #jQuery
jQuery单页面文字搜索插件jquery.fullsearch.js的使用方法
Feb 04 #jQuery
9种方法优化jQuery代码详解
Feb 04 #jQuery
You might like
使用MaxMind 根据IP地址对访问者定位
2006/10/09 PHP
php UBB 解析实现代码
2011/11/27 PHP
关于php 接口问题(php接口主要也就是运用curl,curl函数)
2013/07/01 PHP
ini_set的用法介绍
2014/01/07 PHP
PHP5.2下preg_replace函数的问题
2015/05/08 PHP
jQuery 标题的自动翻转实现代码
2009/10/14 Javascript
window.location.hash 属性使用说明
2010/03/20 Javascript
防止页面被iframe(兼容IE,Firefox火狐)
2010/07/04 Javascript
多种方法实现JS动态添加事件
2013/11/01 Javascript
Javascript中3种实现继承的方法和代码实例
2014/08/12 Javascript
FF(火狐)浏览器无法执行window.close()解决方案
2014/11/13 Javascript
js实现横向伸展开的二级导航菜单代码
2015/08/28 Javascript
使用JavaScript解决网页图片拉伸问题(推荐)
2016/11/25 Javascript
easyui 中的datagrid跨页勾选问题的实现方法
2017/01/18 Javascript
JS中LocalStorage与SessionStorage五种循序渐进的使用方法
2017/07/12 Javascript
简单实现jQuery手风琴效果
2017/08/18 jQuery
探究react-native 源码的图片缓存问题
2017/08/24 Javascript
解决linux下node.js全局模块找不到的问题
2018/05/15 Javascript
vue异步加载高德地图的实现
2018/06/19 Javascript
解决vue.js this.$router.push无效的问题
2018/09/03 Javascript
JS实现查找数组中对象的属性值是否存在示例
2019/05/24 Javascript
JavaScript 继承 封装 多态实现及原理详解
2019/07/29 Javascript
基于JS+HTML实现弹窗提示是否确认提交功能
2020/06/17 Javascript
纯JS开发baguetteBox.js响应式画廊插件
2020/06/28 Javascript
[31:29]完美世界DOTA2联赛PWL S3 INK ICE vs Magma 第一场 12.20
2020/12/23 DOTA
Python写入CSV文件的方法
2015/07/08 Python
对django中render()与render_to_response()的区别详解
2018/10/16 Python
python pickle存储、读取大数据量列表、字典数据的方法
2019/07/07 Python
HTML5本地存储之Web Storage详解
2016/07/04 HTML / CSS
C语言笔试题
2014/09/04 面试题
材料加工硕士生求职信
2013/10/10 职场文书
2014年清明节网上祭英烈寄语
2014/04/09 职场文书
银行职员自我鉴定
2014/04/20 职场文书
房产分割协议书范文
2014/11/21 职场文书
会计手工模拟做账心得体会
2016/01/22 职场文书
Nginx实现负载均衡的项目实践
2022/03/18 Servers