搜索
博主信息
博文 38
粉丝 1
评论 0
访问量 30094
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
5月10日作业
鲨鱼辣椒的博客
原创
802人浏览过

仿机器人客服自动回复

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>实战: 仿机器人聊天窗口</title>
    <style type="text/css">
        div:nth-child(1) {
            width: 500px;
            height: 650px;
            background-color: lightskyblue;
            margin: 30px auto;
            color: #333;
            box-shadow: 2px 2px 2px #808080
        }

        h2 {
            text-align: center;
            margin-bottom: -10px;
        }
        div:nth-child(2) {
            width: 400px;
            height: 500px;
            border: 4px double green;
            background-color: #efefef;
            margin: 20px auto 10px;
        }

        ul {
            list-style: none;
            line-height: 2em;
            /*border: 1px solid red;*/
            overflow: hidden;
            padding: 15px;
        }

        table {
            width: 90%;
            height:80px;
            margin: auto;
        }

        textarea{
            /*width: 300px;*/
            border: none;
            resize: none;
            background-color: lightyellow;

        }
        button {
            width: 60px;
            height: 40px;
            background-color: seagreen;
            color: white;
            border: none;
            /*text-align: left;*/

        }
        button:hover {
            cursor: pointer;
            background-color: orange;
        }
    </style>
</head>
<body>
<div>
    <h2>在线客服</h2>
    <div contenteditable="true">
        <ul>
            <li></li>
        </ul>
    </div>
    <table>
        <tr>
            <td align="right"><textarea cols="50" rows="4" name="text" autofocus> </textarea></td>
            <td align="left"><button type=button>发送</button></td>
        </tr>
    </table>
</div>
<script type="text/javascript">
    //获取按钮,
    var btn = document.getElementsByTagName('button')[0];
    //文本域
    var text = document.getElementsByName('text')[0];
    //对话框内容
    var list = document.getElementsByTagName('ul')[0];
    // 设置计算器
    var sum = 0;

    // 给按钮添加一个点击事件
    btn.onclick = function (ev) {
        //获取到用户提交内容
        if (text.value.length === 0){
            alert('亲,想跟人家说什么呢');
            return false;
        }
        //用户输入的内容保存到变量中
        var userComment = text.value;
        //清空用户的输入框
        text.value = '';
        //生成一个新节点,留言内容
        var li = document.createElement('li');

        //设置用户头像
        var userPic = '<img src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3826955403,4281563542&fm=26&gp=0.jpg" alt="" width="30" style="border-radius: 50%">';
        li.innerHTML = userPic + userComment;

        //添加到留言列表中
        list.appendChild(li);
        sum += 1;  // 回复内容计数加1


        setTimeout(function () {
            var info = [
                '你好烦人呀,本姑娘好累,不知道怜香惜玉吗?',
                '除了退货,退款,维修,什么问题都可以问',
                '啥事呀,我的帅哥哥'
            ];
            var temp = info[Math.floor(Math.random()*3)];//取1-5之间的一个整数:Math.floor(Math.random()*6 + 1)
            var reply = document.createElement('li');  /*创建新元素用来保存回复内容*/
            var kefuPic = '<img src="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=181484167,948025248&fm=26&gp=0.jpg" width="30" style="border-radius:50%;">';
            reply.innerHTML = kefuPic + '<span style="color:red">'+temp+'</span>';
            // reply.style.float = 'right';

            list.appendChild(reply);  //将回复内容添加到窗口中
            sum += 1;  // 回复内容计数加1
        },1800);

        // 设置回复内容大于十条则清空聊天窗口
        if (sum > 10 ){
            list.innerHTML = '';//清空对话框聊天内容
            sum = 0;//计数器清零
        }

    };
</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学