前端练习题——JS粗简留言板

注意:
1、input等输入框,获取的值在value中,不能用innerText、innerHTML
2、this关键字和java中的使用同理,表示当前对象。把当前对象当作参数传递,接收者直接用变量接收即可使用

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			#title,#username,#content{
				
				width: 150px;
				height: 30px;
				border-radius: 10px;
				background-color: rgb(38,126,86);
				color: white;
				font-size: 20px;
			}
			body{
				font-size: 20px;
				font-weight: 800;
			}
			.li_son{
				width:100% ;
				height: 250px;
				border: 2px gainsboro dashed;
				margin-left: -33px;
				list-style-type: none;
			}
			#name_{
				color: blue;
				text-decoration: underline;
			}
			#remove{
				color: blue;
				text-decoration: underline;
				cursor: pointer;
			}
		</style>
	</head>
	<body>
		<!--用于装新添加的内容(发布的留言内容)-->
		<ul id="ul_father"></ul>


		<!--留言板-->
			<table >
				<tr>
					<td>
						标题:<input type="text" id="title" onfocus="bg(this)" onblur="rebg(this)"/><br/>
					</td>
				</tr>
				<tr>
					<td>
						名字:<input type="text" id="username"  onfocus="bg(this)" onblur="rebg(this)"/><br/>
					</td>
				</tr>
				<tr>
					<td>
						内容:<input type="text" id="content" style="width: 250px;height: 45px;" onfocus="bg(this)" onblur="rebg(this)"/><br/>
					</td>
				</tr>
				<tr>
					<td>
						<input type="button" value="回复" onclick="add()"/>
					</td>
				</tr>
			</table>
	
		
		
		<script language="JavaScript">
			
			var title = document.getElementById("title");
			var username = document.getElementById("username");
			var content = document.getElementById("content");
			
			//装添加留言内容的容器对象
			var ul_father = document.getElementById("ul_father");
			
			//给input框设置动态效果,因为大量重复,使用this去重
			function bg(obj){
				obj.style.backgroundColor = "rgb(255,240,0)";
				
				if(obj != document.getElementById("content")){
					obj.style.width = "250px";
					obj.style.height = "45px";
				}else{
					obj.style.width = "350px";
					obj.style.height = "55px";
				}
			}
			function rebg(obj){
				obj.style.backgroundColor = "rgb(38,126,86)";
				
				if(obj != document.getElementById("content")){
					obj.style.width = "150px";
					obj.style.height = "30px";
				}else{
					obj.style.width = "250px";
					obj.style.height = "45px";
				}
			}
			
			
			//给ul添加li列表(添加每一条内容)
			function add(){
				ul_father.innerHTML += "<li class='li_son'> <h1>标题:"+ title.value + "</h1><h3>名字:<span id='name_'>"+ username.value + "</span></h3><h3>内容:"+content.value+"</h3><span id='remove' οnclick='shanchu(this)'>删除</span></li>" ;
			}
			
			
			
			//删除,每次点击获取的li只有一个,索引始终为0
			function shanchu(obj){
				obj.parentNode.remove();
			}
			
		</script>
	</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈年_H

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值