jquery实现聊天机器人

 更新时间:2020年02月08日 10:44:36   作者:哪天才能学到vue  
这篇文章主要为大家详细介绍了jquery实现聊天机器人,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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弹出层始终垂直居中相对于屏幕或当前窗口

    碰到没有固定高或者固定宽或者固定高和宽的时候,我们就需要用JS去处理,去动态获取当前窗口高或者宽;今天弄了2种情况,一个是相对于屏幕窗体,一个是相对于当前的窗口,看代码
    2013-04-04
  • jquery实现的一个导航滚动效果具体代码

    jquery实现的一个导航滚动效果具体代码

    首页有一个导航页面要实现滚动效果,索性就仿造别人的效果自己做了一个,大体上还行,看起来还是比较流畅的,现滚动效果,脚本代码如下,感兴趣的朋友可以参考下
    2013-05-05
  • jQuery.position()方法获取不到值的安全替换方法

    jQuery.position()方法获取不到值的安全替换方法

    这篇文章主要介绍了jQuery.position()方法获取不到值的安全替换方法,本文给出了一种变通的方法,用.offset()来换算,需要的朋友可以参考下
    2015-03-03
  • js判断checkbox是否选中个数的方法(超简单)

    js判断checkbox是否选中个数的方法(超简单)

    下面小编就为大家带来一篇js判断checkbox是否选中个数的方法(超简单)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • 创建自己的jquery表格插件

    创建自己的jquery表格插件

    这篇文章主要介绍了一个自己创建的jquery表格插件,实现了基本的json数据的动态加载,自动分页,全选全不选,鼠标悬浮变色,隔行变色等,感兴趣的小伙伴们可以参考一下
    2015-11-11
  • jquery操作复选框checkbox的方法汇总

    jquery操作复选框checkbox的方法汇总

    这篇文章主要介绍了jquery操作复选框checkbox的方法,实例汇总了jQuery针对checkbox进行判断与赋值的各种常用操作技巧,需要的朋友可以参考下
    2015-02-02
  • jquery实现可关闭的倒计时广告特效代码

    jquery实现可关闭的倒计时广告特效代码

    这篇文章主要介绍了jquery实现可关闭的倒计时广告特效代码,涉及jquery计时器及鼠标事件动态操作页面元素样式的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • 表头固定(利用jquery实现原理介绍)

    表头固定(利用jquery实现原理介绍)

    表头固定应该是一个用得比较多的功能,参考了网上几个例子,在几个常用浏览器下显示不是很完美
    2012-11-11
  • jquery实现获取具体时间(年月日)后3个月+1天的年月日

    jquery实现获取具体时间(年月日)后3个月+1天的年月日

    这篇文章主要为大家详细介绍了jquery如何实现获取具体时间(年月日)后3个月+1天的年月日,文中的示例代码讲解详细,有需要的小伙伴可以参考一下
    2023-11-11
  • jQuery stop()用法实例详解

    jQuery stop()用法实例详解

    一直对stop的用法一知半解,今天抽点时间学习下jQuery stop()用法实例详解的相关知识,特此分享脚本之家平台,供大家参考
    2016-07-07

最新评论