用jQuery写发微博

要求

1、点击发出的“微博”有发送的时间,年月日时分秒
2、最新发出的“微博放在第一条”
3、有删除“微博”功能

1、html代码

			<h2>我的微博</h2>
			<textarea name="" id="txt" cols="70" rows="6"></textarea>
			<button id="btn">发布</button>
			
			<ul id="list"></ul>	

css代码

		body{
			background: #f2f2f2f2;
		}
			h2{ 
				display: block;
				margin: 0 auto;
				text-align: center;
			}
			#txt{
				display: block;
				margin: 0 auto;
				margin-top: 30px;
			}
			p{
				word-break: break-all;
				margin: 0;
			}
		
			#btn{
				width: 60px;
				height: 30px;
				display: block;
				margin-left: 650px;
				margin-top: 10px;
				background: antiquewhite;
				/*border-radius: 10px;*/
				/*box-shadow: none;*/
			}
			#con{
				width: 500px;
				height:100px;
				border: 1px solid #999999;
				margin: 0 auto;
			}
			#list{
				width: 530px;
				/*height: 100px;*/
				margin: 0 auto;
				list-style: none;
			}
			#list li{
				width: 530px;
				min-height: 100px;	
				background:white;	
				border: 1px solid #999999;		
				list-style: none;
				position: relative;
				padding:5px 5px 20px 5px;
				box-sizing: border-box;
				margin-top: 10px;
			}
			.dli{
				position: absolute;
				right: 10px;
				bottom: 0;
			}
			.time{
				position: absolute;
				bottom: 0;
				color: #999999;
			}

3、js的代码

$('#btn').click(function(){
					var text1=$('#txt').val();
					//点击后清空文本域内容
					$('#txt').val("");
					
					if(text1==""){
						alert("请输入内容!")
					}else{
						var date=new Date();
						var y=date.getFullYear();
						var m=date.getMonth();
						var d=date.getDate();
						var h=date.getHours();
						var min=date.getMinutes();
						var s=date.getSeconds();
						var time=y+'-'+m+'-'+d+' '+h+':'+min+':'+s;										
						var a=`<li>
									<p>${text1}</p>
									<span class="time">${time}</span>
									<button class="dli" onclick="del(this)">删除</button>
								</li>`;
						$('#list').prepend(a);
					}	
				})					
				function del(a){
					a.parentNode.parentNode.removeChild(a.parentNode);
				}

分析js代码

1、如何拿到时间
						var date=new Date();//拿到系统时间
						var y=date.getFullYear();//从系统时间中拿到年分
						var m=date.getMonth();//取得月份
						var d=date.getDate();//取得日
						var h=date.getHours();//小时
						var min=date.getMinutes();//分钟
						var s=date.getSeconds();//秒
2、将内容加到微博主题上
var a=`<li>
		<p>${text1}</p>
		<span class="time">${time}</span>
		<button class="dli" onclick="del(this)">删除</button>
	</li>`;
$('#list').prepend(a);

用模板字符串写微博
a.prepend(b)是指将b放到a的最前面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值