JQuery制作论坛发帖

 效果图

每次发布都保证新发布的帖子在最前面,关键代码

//创建li标签
var li_tag = $("<li></li>");
//将li插入到ul里
$(".bbs section ul").prepend(li_tag);

 随机生成头像,用到Math.floor(random())

//将头像存入数组
var pic = new Array("tou01.jpg","tou02.jpg","tou03.jpg","tou04.jpg");
//获取随机头像
var pic_tag = Math.floor(Math.random()*4);
//创建头像节点
var picture = $("<div><img src=images/" + pic[pic_tag] + "></div>");
//插入头像
$(li_tag).append(picture);

获取发布时间以及版块

//获取时间
var nowDate = new Date();
var currentDate = nowDate.getFullYear()+"-"+parseInt(nowDate.getMonth()+1)+"-"+nowDate.getDate()+" "+nowDate.getHours()+":"+nowDate.getMinutes();
//在p节点中插入版块,并获取内容
$(p_tag).append("<span>版块:"+ $(".post select").val()+"</span>");
//在P节点中插入发布时间
$(p_tag).append("<span>发表时间:"+ currentDate+"</span>");
//插入头像
$(li_tag).append(picture);
//插入标题
$(li_tag).append(title);
//插入版块、时间
$(li_tag).append(p_tag);

 完整代码

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>课工场论坛列表</title>
    <link href="css/main.css" rel="stylesheet">
	<script src="js/jquery-1.12.4.js" type="text/javascript"></script>
</head>
<body>
<div class="bbs">
    <header><span>我要发帖</span></header>
    <section>
        <ul></ul>
    </section>
    <div class="post">
        <input class="title" placeholder="请输入标题(1-50个字符)">
        所属版块:<select><option>请选择版块</option><option>电子书籍</option><option>新课来了</option><option>新手报到</option><option>职业规划</option></select>
        <textarea class="content"></textarea>
        <input class="btn" value="发布">
    </div>
</div>
<script type="text/javascript">
		$(function(){
			$(".bbs header span").click(function(){
				$(".post").show();
			});
			var pic = new Array("tou01.jpg","tou02.jpg","tou03.jpg","tou04.jpg");
			$(".post .btn").click(function(){
				var li_tag = $("<li></li>");//创建li标签
				var pic_tag = Math.floor(Math.random()*4);//获取随机头像
				var picture = $("<div><img src=images/" + pic[pic_tag] + "></div>");//创建头像节点
				var title = $("<h1>"+ $(".title").val() + "</h1>");//创建title标题节点,并获取输入内容
				var p_tag = $("<p></p>");//创建p标签
				var nowDate = new Date();//获取时间
				var currentDate = nowDate.getFullYear()+"-"+parseInt(nowDate.getMonth()+1)+"-"+nowDate.getDate()+" "+nowDate.getHours()+":"+nowDate.getMinutes();
				$(p_tag).append("<span>版块:"+ $(".post select").val()+"</span>");//在p节点中插入版块,并获取内容
				$(p_tag).append("<span>发表时间:"+ currentDate+"</span>");//在P节点中插入发布时间
				$(li_tag).append(picture);//插入头像
				$(li_tag).append(title);//插入标题
				$(li_tag).append(p_tag);//插入版块、时间
				$(".bbs section ul").prepend(li_tag);//将li插入到ul里
				$(".post").hide();
			})
		})
	</script>
</body>
</html>

css样式

	<style type="text/css">
		*{margin: 0; padding: 0; font-family: "Arial";}
		ul,li{list-style: none;}
		.bbs{margin: 0 auto; width: 600px; position: relative;}
		header{padding: 5px 0; border-bottom: 1px solid #cecece;}
		header span{display:inline-block; width: 220px; height: 50px; color: #fff; background: #009966; font-size: 18px; font-weight: bold; text-align: center;line-height: 50px; border-radius: 8px; cursor: pointer;}
		.post{position: absolute; background: #ffffff; border: 1px #cccccc solid; width: 500px; left: 65px; top:70px; padding: 10px; font-size: 14px; z-index: 999999; display: none;}
		.post .title{width: 450px; height:30px; line-height: 30px; display: block; border: 1px #cecece solid; margin-bottom: 10px;}
		.post select{width: 200px; height: 30px;}
		.post .content{width: 450px; height: 200px; display: block; margin: 10px 0;border: 1px #cecece solid;}
		.post .btn{width: 160px; height: 35px; color: #fff; background: #009966; border: none; font-size: 14px; font-weight: bold; text-align: center; line-height: 35px; border-radius: 8px; cursor: pointer;}
		
		.bbs section ul li{padding: 10px 0; border-bottom: 1px #999999 dashed;
		    overflow: hidden;}
		.bbs section ul li div{float: left; width: 60px; margin-right: 10px;}
		.bbs section ul li div img{ border-radius:50%; width: 60px;}
		.bbs section ul li h1{float: left; width: 520px; font-size: 16px; line-height: 35px;}
		.bbs section ul li p{color: #666666; line-height: 25px; font-size: 12px; }
		.bbs section ul li p span{padding-right:20px;}
	</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值