一、
技能训练1:制作论坛帖子页面
代码:
1.html文件
<link href="css/bbs.css" rel="stylesheet">
<script type="text/javascript" src="jquery-3.3.1.js"></script>
<script type="text/javascript">
(".bbs header span").click(function(){
$(".bbs .post").show();
})
$(".post .btn").click(function(){
//准备内容
var touArr=new Array("tou01.jpg","tou02.jpg","tou03.jpg","tou04.jpg");
var inum=Math.floor(Math.random()*4);//随机获取头像
var touimg=touArr[inum];
var titleContent=$(".post .title").val();
var bankuaiContent=$(".post select").val();
var mydate=new Date();
var currentDate=mydate.getFullYear()+"-"+(mydate.getMonth()+1)+"-"+mydate.getDate()+" "+mydate.getHours()+":"+mydate.getMinutes();
var $newLi=$("<li></li>");//创建一个新的li节点元素
var $touimg=$("<div><img src='images/"+touimg+"'/></div>");//创建头像节点
var $title=$("<h1 class='title'>"+titleContent+"</h1>");//设置标题节点
var newp=$("<p></p>");//创建新的p节点元素
$(newp).append("<span>版块:"+bankuaiContent+"</span>");//在p节点中插入版块
$(newp).append("<span>发表时间:"+currentDate+"</span>");
$($newLi).append($touimg);//插入头像
$($newLi).append($title);//插入标题
$($newLi).append(newp);//插入版块、时间内容
$(".bbs section ul").prepend($newLi);
$(".post .content").val("");
//清空与隐藏
$(".post .title").val("");
$(".post").hide();
})
//清空与隐藏
$(".title").val("");
$("select option:first").attr("selected","selected");
$(".content").val();
$(".post").hide();
});
</script>
</head>
<body>
<div class="bbs">
<header><span>我要发帖</span></header>
<section>
<ul>
<li>
<div><img src="images/tou01.jpg"></div>
<h1 class="title">首发式</h1>
<p>
<span>版块:</span>
<span>发布时间:</span>
</p>
</li>
</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>
</body>
</html>
2.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 #999999 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 #aaaaaa solid; margin-bottom: 10px;}
.post select{width: 200px; height: 30px;}
.post .content{width: 450px; height: 200px; display: block; margin: 10px 0;border: 1px #aaaaaa 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;}