jquery实现聊天机器人


Posted in jQuery onFebruary 08, 2020

本文实例为大家分享了jquery实现聊天机器人的具体代码,供大家参考,具体内容如下

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="./demo.css" rel="external nofollow" >
</head>

<body>
  <div class="wrapper">
    <h4 class="header">俊凯</h4>
    <div class="content">
      <div class="mine">
        <img src="./image/5.jpg" alt="">
        <div class="text">
          今天天气怎么样
        </div>
      </div>
      <div class="robot">
        <img src="./image/5.jpg" alt="">
        <div class="text">
          天气很好呀适合出门呢~~
        </div>
      </div>
    </div>
    <div class="inp">
      <input type="text" id="word">
      <button id="submit">发送</button>
    </div>

  </div>
  <script src="./jquery.js"></script>
  <script src="./demo.js"></script>
  
  <script>
    
  
  </script>

  
</body>
</html>

CSS:

* {
  padding: 0;
  margin: 0;
}

::-webkit-scrollbar {
  width: 0px;
}
html, body {
  height: 100%;
}
.wrapper {
  width: 600px;
  margin: 0 auto;
  border: 1px solid #eee;
  height: 100%;
  position: relative;
  background-color: #eee;
  /* overflow: hidden; */
}
.wrapper .content {
  /* overflow-x: hidden;
  overflow-y: scroll; */
  overflow: auto;
  height: calc(100% - 110px);
  line-height: 30px;
  padding: 10px;
}
.wrapper .header {
  background-color: grey;
  text-align: center;
  color: #fff;
  height: 40px;
  line-height: 40px;
  font-weight: 700;
}

.wrapper .content .mine {
  float: right;
  width: 400px;
}
.wrapper .content .robot {
  float: left;
  width: 400px;
}
.wrapper .content img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  vertical-align: middle;
}
.content .mine img {
  float: right;
}
.content .mine .text {
  float: right;
  background-color: greenyellow;
}
.content .robot img {
  float: left;
}
.content .robot .text {
  float: left;
  background-color: #fff;
}
.text {
  max-width: 250px;
  font-size: 16px;
  padding: 0 10px;
  border-radius: 3px;
  /* border: 1px solid #fff; */
}
.inp {
  width: 100%;
  height: 50px;
  line-height: 50px;
  position: absolute;
  bottom: 0px;
  font-size: 0;
  text-align: center;
  /* padding: 0 10px; */
  background-color: #ddd;
  /* vertical-align: middle; */
}

.inp input {
  width: calc(100% - 80px);
  height: 30px;
  line-height: 30px;
  border: none;
  outline: none;
  font-size: 14px;
  display: inline-block;
  vertical-align: middle;
}
.inp button {
  width: 60px;
  height: 30px;
  font-size: 14px;
  border: none;
  outline: none;
  background-color: #ccc;
  display: inline-block;
  vertical-align: middle;
  cursor: pointer;
}

js:

$('#submit').click(function(){
  var val = $('#word').val();
  if(val){
    renderDom('mine',val)
    $('#word').val('')
     $.ajax({
      type:'GET',
      url:'http://temp.duyiedu.com/api/chat',
      data:{
        text:val
      },
      dataType:'json',
      success:function(res){
        // console.log(res)
        renderDom('robot',res.text);
      }
    })
  }
})
$('#word').on('keyup',function (e){
  if(e.keyCode == 13){
    $('#submit').click()
  }
})
function renderDom(role,text){
  $(`
  <div class="${role}">
  <img src="./image/${role == 'mine' ? '5.jpg' : '7.jpg'}" alt="">
  <div class="text">
    ${text}
  </div>
</div>`).appendTo($(`.content`));
var scrollHeight = $('.content')[0].scrollHeight;
var contentHeight = $('.content')[0].offsetHeight;
$('.content').scrollTop(scrollHeight-contentHeight);


}

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

jQuery 相关文章推荐
HTML5+jQuery实现搜索智能匹配功能
Mar 24 jQuery
jquery中$.fn和图片滚动效果实现的必备知识总结
Apr 21 jQuery
jQuery实现下拉菜单的实例代码
Jun 19 jQuery
jQuery实现简单的手风琴效果
Apr 17 jQuery
基于jQuery实现手风琴菜单、层级菜单、置顶菜单、无缝滚动效果
Jul 20 jQuery
jQuery Ajax向服务端传递数组参数值的实例代码
Sep 03 jQuery
jQuery EasyUI开发技巧总结
Sep 26 jQuery
jQuery轻量级表单模型验证插件
Oct 15 jQuery
html+jQuery实现拖动滑块图片拼图验证码插件【移动端适用】
Sep 10 jQuery
layui+jquery支持IE8的表格分页方法
Sep 28 jQuery
jQuery实现雪花飘落效果
Aug 02 jQuery
jquery实现鼠标悬浮弹出气泡提示框
Dec 23 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
jQuery实现小火箭返回顶部特效
Feb 03 #jQuery
JQuery事件委托(适用于给动态生成的脚本元素添加事件)
Feb 01 #jQuery
You might like
PHP树的深度编历生成迷宫及A*自动寻路算法实例分析
2015/03/10 PHP
php中使用websocket详解
2016/09/23 PHP
php preg_match的匹配不同国家语言实例
2016/12/29 PHP
script的async属性以非阻塞的模式加载脚本
2013/01/15 Javascript
浅析tr的隐藏和显示问题
2014/03/05 Javascript
javascript事件函数中获得事件源的两种不错方法
2014/03/17 Javascript
js动态改变select选择变更option的index值示例
2014/07/10 Javascript
分享一个自己写的简单的javascript分页组件
2015/02/15 Javascript
使用AngularJS中的SCE来防止XSS攻击的方法
2015/06/18 Javascript
js html5 css俄罗斯方块游戏再现
2016/10/17 Javascript
jquery DataTable实现前后台动态分页
2017/06/17 jQuery
jQuery实现的弹幕效果完整实例
2017/09/06 jQuery
jQuery实现IE输入框完成placeholder标签功能的方法
2017/09/20 jQuery
微信小程序项目实践之九宫格实现及item跳转功能
2018/07/19 Javascript
JavaScript实现PC端横向轮播图
2020/02/07 Javascript
[40:27]完美世界DOTA2联赛PWL S3 PXG vs GXR 第一场 12.19
2020/12/24 DOTA
Python版微信红包分配算法
2015/05/04 Python
python使用matplotlib绘图时图例显示问题的解决
2017/04/27 Python
Python 3.6 读取并操作文件内容的实例
2018/04/23 Python
对python mayavi三维绘图的实现详解
2019/01/08 Python
python快排算法详解
2019/03/04 Python
Python Request爬取seo.chinaz.com百度权重网站的查询结果过程解析
2019/08/13 Python
python 利用pyttsx3文字转语音过程详解
2019/09/25 Python
Python全局锁中如何合理运用多线程(多进程)
2019/11/06 Python
python中wx模块的具体使用方法
2020/05/15 Python
在weblogic中发布ejb需涉及到哪些配置文件
2012/01/17 面试题
写求职信有什么意义
2014/02/17 职场文书
优秀护士获奖感言
2014/02/20 职场文书
农林环境专业求职信
2014/03/13 职场文书
敬老模范事迹
2014/05/21 职场文书
服务承诺书怎么写
2014/05/24 职场文书
群众路线个人对照检查材料
2014/09/23 职场文书
公司停电通知
2015/04/15 职场文书
培训讲师开场白
2015/06/01 职场文书
失恋33天观后感
2015/06/11 职场文书
MySQL安装失败的原因及解决步骤
2022/06/14 MySQL