Blogger Information
Blog 38
fans 1
comment 0
visits 24257
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5月10日作业
鲨鱼辣椒的博客
Original
614 people have browsed it

仿机器人客服自动回复

实例

<!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>

运行实例 »

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


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post