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 相关文章推荐
基于jQuery实现瀑布流页面
Apr 11 jQuery
关于jQuery库冲突的完美解决办法
May 20 jQuery
jQuery插件ImgAreaSelect实现头像上传预览和裁剪功能实例讲解一
May 26 jQuery
JQuery form表单提交前验证单选框是否选中、删除记录时验证经验总结(整理)
Jun 09 jQuery
jQuery自定义多选下拉框效果
Jun 19 jQuery
jquery对table做排序操作的实例演示
Aug 10 jQuery
通过jquery获取上传文件名称、类型和大小的实现代码
Apr 19 jQuery
jQuery实现百度图片移入移出内容提示框上下左右移动的效果
Jun 05 jQuery
jQuery实现提交表单时不提交隐藏div中input的方法
Oct 08 jQuery
jQuery实现图片随机切换、抽奖功能(实例代码)
Oct 23 jQuery
jquery实现轮播图特效
Apr 12 jQuery
jQuery实现本地存储
Dec 22 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学习之 认清变量的作用范围
2010/01/26 PHP
php数组合并的二种方法
2014/03/21 PHP
PHP实现的简单操作SQLite数据库类与用法示例
2017/06/19 PHP
PHP获取真实IP及IP模拟方法解析
2020/11/24 PHP
js不能跳转到上一页面的问题解决方法
2013/03/01 Javascript
js操作iframe的一些方法介绍
2013/06/25 Javascript
javascript编程异常处理实例小结
2015/11/30 Javascript
jQuery实现的调整表格行tr上下顺序
2016/01/10 Javascript
vue2滚动条加载更多数据实现代码
2017/01/10 Javascript
详解vue-cli + webpack 多页面实例应用
2017/04/25 Javascript
ES6 迭代器与可迭代对象的实现
2019/02/11 Javascript
layui使用表格渲染获取行数据的例子
2019/09/13 Javascript
利用Python实现简单的相似图片搜索的教程
2015/04/23 Python
python实现的用于搜索文件并进行内容替换的类实例
2015/06/28 Python
详解python基础之while循环及if判断
2017/08/24 Python
利用Python爬取微博数据生成词云图片实例代码
2017/08/31 Python
python Flask实现restful api service
2017/12/04 Python
利用Python找出序列中出现最多的元素示例代码
2017/12/08 Python
人脸识别经典算法一 特征脸方法(Eigenface)
2018/03/13 Python
Python基于time模块表示时间常用方法
2020/06/18 Python
python 用Matplotlib作图中有多个Y轴
2020/11/28 Python
DogBuddy荷兰:找到你最完美的狗保姆
2019/04/17 全球购物
简述索引存取方法的作用和建立索引的原则
2013/03/26 面试题
95%的面试官都会问到的50道Java线程题,附答案
2012/08/03 面试题
假日旅行社实习自我鉴定
2013/09/24 职场文书
电子商务专业求职信
2014/03/08 职场文书
党课培训心得体会
2014/09/02 职场文书
现实表现材料范文
2014/12/23 职场文书
校本研修个人总结
2015/02/28 职场文书
吴仁宝观后感
2015/06/09 职场文书
《这片土地是神圣的》教学反思
2016/02/16 职场文书
慰问信(范文3篇)
2019/10/23 职场文书
导游词之吉林吉塔
2019/11/11 职场文书
Python数据可视化之用Matplotlib绘制常用图形
2021/06/03 Python
如何理解python接口自动化之logging日志模块
2021/06/15 Python
win11电脑关机鼠标灯还亮怎么解决? win11关机后鼠标灯还亮解决方法
2023/01/09 数码科技