博主信息
博文 87
粉丝 0
评论 0
访问量 72826
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
第十二节课作业:2.仿聊天机器人
黄忠倚的博客
原创
1229人浏览过

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>第十二节课作业:2.仿聊天机器人</title>
	<style type="text/css">
		body {
			font-family: microsoft yahei;
		}
		div:nth-child(1) {
			width: 450px;
			height: 650px;
			background-color: lightskyblue;
			margin:30px auto;
			color: black;
			box-shadow: 2px 2px 2px #808080;
		}
		h2 {
			text-align: center;
			margin-bottom: -10px;
		}
		div:nth-child(2) {
			width: 400px;
			height: 500px;
			border:4px double seagreen;
			background-color: #f5f5f5;
			margin:20px auto 10px;
		}
		ul {
			list-style-type: none;
			line-height: 2em;
			overflow: hidden;
			padding:15px;
		}
		table {
			width: 90%;
			height: 80px;
			margin:auto;
		}
		textarea {
			width: 335px;
			resize:none;
			border:none;
			background-color: lightyellow;
			margin:0;
			padding:0;
			text-indent: 0.3em;
		}
		button {
			border-radius: 10%;
			width: 60px;
			height: 40px;
			background-color: seagreen;
			color: #fff;
			border:none;
			margin-left:5px;
		}
		button:hover {
			cursor: pointer;
			background-color:orange;
			font-size: 1.02em;

		}
	</style>
</head>
<body>
		<div>
			<h2>我与老婆的对话</h2>
			<div contenteditable="false">
				<ul>
					<li></li>
			</ul>
		</div>
			<table>
				<tr>
					<td><textarea name="text" cols="50" rows="4" autofocus="autofocus"></textarea></td>
					<td><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 (){
		if (text.value.length == 0){
			alert('黄忠倚,现在几点啦,你怎么还不睡觉,第二天不用上班吗?')
			return false
		}
		onkeydown="if(event.keyCode==13){gosubmit();}" 
			var myComment = text.value
			text.value = ''

			//创建新节点
			var li = document.createElement('li')
			var myPic = '<img src="./images/hzy.jpg" width="30" style="border-radius:50%">' 
			li.innerHTML = myPic +'<span style="background-image:url(./images/bg.png);padding:10px 10px 10px 15px;" >' + myComment + '</span>'

			//插到页面中:父级.appendChild()
			list.appendChild(li)
			sum += 1

			setTimeout(function(){
				var info = ['每晚都这样','奶瓶又不烫','我真是火冒三丈','想打人都有份','你马上给我滚出去!']
				var temp = info[Math.floor(Math.random()*5)]
				var reply = document.createElement('li')
				var herPic = '<img src="./images/laopo.jpg" width="30" style="border-radius:50%">'
			reply.innerHTML = herPic+'<span style="color:red;background-image:url(./images/bg.png);padding:10px 10px 10px 15px;" >'+ temp + '</span>'
			list.appendChild(reply)
			sum += 1
			},2000)
 
			if (sum > 10) {
				list.innerHTML =''
				sum = 0
			}
		}
	</script>
</body>
</html>

运行实例 »

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

作业地址:http://mi-888.com/PHP/zuoye/20180330/3.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+教程免费学