Blogger Information
Blog 33
fans 0
comment 0
visits 24373
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
DOM模拟智能对话机器人 20180914 23:00
EmonXu的博客
Original
649 people have browsed it

利用DOM操作,我们可以模拟智能对话。

代码如下,未美化,仅实现功能。

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>聊天机器人</title>
</head>
<body>
<div>
    <h2>
        在线***
    </h2>
    <div>
        <ul>
        </ul>
    </div>
    <table>
        <tr>
            <td align='right'><textarea name="text" clos="50" rows="4"></textarea></td>
            <td align="left"><button name="btn">发送</button> </td>
        </tr>
    </table>
</div>
<script type="text/javascript">
    let say=document.getElementsByName('text')[0];
    let btn=document.getElementsByName("btn")[0];
    let ul=document.getElementsByTagName("ul")[0];

    btn.onclick=function(){
        let li=document.createElement("li");
        li.innerHTML=say.value;
        ul.appendChild(li);
        //立即清空用户信息区
        say.value = '';

        setTimeout(function () {
            let info = [
                '不想理你',
                '叔叔我们不约',
                '再说一遍',
                '去洗澡了',
                '下次吧,谢谢'
            ];
            let temp = info[Math.floor(Math.random()*4)]

            let li1=document.createElement("li");
            li1.innerHTML='<span style="color:red">'+temp+'</span>';
            ul.appendChild(li1);

        },2000)

    }

</script>

</body>
</html>

运行实例 »

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



Correction status:qualified

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