批改状态:合格
老师批语:
本来是0401就写好的但是周日身体不舒服,就没手写,今天感觉不管好不好,不写都不好了!!!
这次写的是一个智能的随机聊天的机器人,娱乐成分大,关于老板之类的大家一笑而过!!!
说下几个关键点或者说是注意事项吧
创建一个随机的数组,提前录好你要回复的信息
将输入框的内容或随机的内容展示出来
展示随机的内容需要加个setTimeout 定时器 延迟展示
当内容数量过多是清空
现在来看看实例吧
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>两个手机</title>
<style type="text/css">
#box{
width: 250px;
height: 450px;
border-radius: 20px;
border: 1px solid #ccc;
background-color: #00bcd4;
margin:50px auto;
}
h5{
text-align: center;
margin: 6px auto;
}
#box1{
width: 230px;
height: 360px;
background-color: #fff;
margin: auto;
padding: 3px;
}
#box2{
width: 230px;
height: 20px;
margin:9px auto;
}
#txt{
height: 24px;
border-radius: 9px;
}
#btn{
height: 30px;
width: 50px;
border-radius: 9px;
}
p{
margin: 0;
padding: 2px;
line-height: 1.2em;
}
.left{
clear: both;
float: left;
color: #00bcd4;
}
.right{
clear: both;
float: right;
color: #ffc107;
}
</style>
</head>
<body>
<div id="box">
<h5>傻逼老板</h5>
<div id="box1">
</div>
<div id="box2">
<input type="text" name="" id="txt">
<button id="btn">发送</button>
</div>
</div>
<script type="text/javascript">
var txt=document.getElementById('txt')
var oBtn=document.getElementById('btn')
var oBox=document.getElementById('box1')
//用来计数当p的数目达到14时清空。
var sum=0
oBtn.onclick=function(){
if(txt.value==''){
txt.value="老板你是傻逼!"
}
//创建一个p标签
var p=document.createElement('p')
//加个class
p.className='left'
//p标签里内容
p.innerHTML='我:' + txt.value
//放到oBox里
oBox.appendChild(p)
//清空输入框
txt.value=''
sum +=2
setTimeout(function(){
info=['是的我是','我是傻逼','是我不对','我是...','我不是人','我对不起你们大家','你打死我吧','我该死','今天我就解散公司','我去裸奔']
tamp=info[Math.floor(Math.random()*10)]
var pp=document.createElement('p')
pp.className='right'
pp.innerHTML= tamp + ' :老板'
oBox.appendChild(pp)
},1000)
//用来计数当p的数目达到14时清空。同时sum归零
if(sum==14){
oBox.innerHTML=' '
sum=0
}
}
</script>
</body>
</html>点击 "运行实例" 按钮查看在线实例
手写加强记忆




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