新浪微博
html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>新浪微博</title>
<link rel="stylesheet" type="text/css" href="css/index.css"/>
<script src="js/jquery-1.12.4.js" type="text/javascript" charset="utf-8"></script>
//注意这个jQuery的引用一定要在index.js之前,否则index.js将使用不了jQuery
<script src="js/index.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="nav">
<img src="images/nav.png">
</div>
<div class="content">
<img src="images/left.png" class="left">
<div class="center">
// textarea和<input type=text>的区别,前者是多行文本框,后者是单行文本框
<textarea class="comment"></textarea>
//disabled是让按钮不可用
<input type="button" name="" id="" value="发布" class="publish" disabled/>
</div>
<img src="images/right.png" class="right">
<div class="messageList">
<div class="info">
<p class="infoText">[腾讯微视短视频获重磅更新:打通QQ音乐由晚上,腾讯对旗下短视频应用微视进行了大版本更新,安卓版直接升级到了4.0片更新推出了视频跟拍、歌词字幕、一键美型等三大功能...
</p>
<p class="infoOperation">
<span class="infoTime">2020-4-15 19:4:43</span>
<span class="infoHandle">
//<a href="#">0</a>若这样写,那么在点击a标签后页面会返回顶部,若什么都不写,则每点击一次就会刷新
<a href="javascript:;">0</a>
<a href="javascript:;">0</a>
<a href="javascript:;">删除</a>
</span>
</p>
</div>
<div class="info">
<p class="infoText">[腾讯微视短视频获重磅更新:打通QQ音乐由晚上,腾讯对旗下短视频应用微视进行了大版本更新,安卓版直接升级到了4.0片更新推出了视频跟拍、歌词字幕、一键美型等三大功能...
</p>
<p class="infoOperation">
<span class="infoTime">2020-4-15 19:4:43</span>
<span class="infoHandle">
<a href="javascript:;">0</a>
<a href="javascript:;">0</a>
<a href="javascript:;">删除</a>
</span>
</p>
</div>
</div>
</div>
<div class="page">
<a href="javascript:;">1</a>
<a href="javascript:;">2</a>
<a href="javascript:;">3</a>
</div>
</body>
</html>
css:
*{
padding: 0;
margin: 0;
}
.nav{
width:100%;
height: 48px;
}
.nav>img{
width: 100%;
}
body{
background: url(../images/body_bg.jpg) no-repeat center 0;/* center:水平;0:垂直 */
}
.content{
width: 1000px;
margin: 200px auto 0 auto;
background-color:#ebdbd4 ;
overflow: hidden;
}
.content>.left{
float: left;
width: 150px;
}
.content>.center{
float: left;
width: 600px;
height: 168px;
background: url(../images/comment.png) 0 0;
background-size:600px 168px;
}
.content>.right{
float: right;
width: 240px;
}
textarea{
resize: none;
width: 570px;
height: 73px;
margin-top: 45px;
margin-left: 15px;
/* background-color: aqua; */
border: none;
outline: none;
}
.publish{
width: 82px;
height: 30px;
margin-top: 4px;
margin-left: 506px;
border: none;
background: #fd8040;
outline: none;
color: white;
}
.messageList{
float: left;
width: 600px;
background-color: #fff;
}
.messageList>.info{
padding :10px 20px;
border-bottom:2px solid #ccc;
}
.info>.infoText{
font-size: 16px;
color: #000;
}
.infoOperation>.infoTime{
font-size: 13px;
color: #ccc;
}
.infoOperation>.infoHandle{
float: right;
}
.infoHandle>a{
font-size: 13px;
text-decoration: none;
color: #ccc;
padding-left: 20px;
margin-left: 10px;
background: url(../images/icons.png) no-repeat 0 0;
}
.infoHandle>a:nth-child(2){
background-position: 0 -17px;
}
.infoHandle>a:nth-child(3){
background-position: 0 -34px;
}
.page{
width: 1000px;
height: 80px;
background-color: #000000;
padding: 10px;
margin: 0 auto;
/*
box-sizing属性可以为三个值之一:content-box(default),border-box,padding-box。
content-box:border和padding不计算入width之内
padding-box:padding计算入width内
border-box:border和padding计算入width实际之内width为980px;
*/
text-align: right;
}
.page>a{
width: 30px;
height: 30px;
display: inline-block;
line-height: 30px;
text-align: center;
margin: 10px;
background-color: none;
border: 1px solid #fff;
color: #fff;
text-decoration: none;
}
js:
/* 由于无法实时监听textarea的内容变化,所以需要通过委托事件来达到效果
其中propertychange针对IE浏览器,input针对其他
(1) change事件—触发该事件必须满足两个条件:
当前对象属性改变,并且是由键盘或鼠标事件激发的(脚步触发无效)
当前对象失去焦点(onblur)
(2) keypress 就是只能监听键盘事件,鼠标复制粘贴操作无能为力
(3) propertychange(ie) 和 input 事件
input 是标准的浏览器事件,一般应用于input元素,当 input 的 value 发生变化就会触发,无论是键盘输入还是鼠标粘贴的 改变都能及时监听到变化。
propertychange 只要当前对象属性发生改变就会触发。(IE专属的) */
$(function(){
// 0.监听内容的时时输入
$("html").delegate(".comment", "propertychange input", function () {
//if($(".comment").val().length>0){
// 判断是否输入了内容
if($(this).val().length>0){
// 让按钮可用
$(".publish").prop("disabled",false);
}else{
// 让按钮不可用
$(".publish").prop("disabled",true);
}
});
// 1.监听发布按钮的点击
$(".publish").click(function () {
// 拿到用户输入的内容
var $text = $(".comment").val();
// 根据内容创建节点
var $weibo = creatE($text);
//点击按钮,重置或者清空textarea中的数据
$(".comment").val("");
// 插入微博
$(".messageList").prepend($weibo);
});
// 2.监听顶点击
$("html").delegate(".top", "click", function () {
//parseInt()把字符串变成数字
$(this).text(parseInt($(this).text())+1);
});
// 3.监听踩点击
$("html").delegate(".dowm", "click", function () {
$(this).text(parseInt($(this).text())+1);
});
// 4.监听删除点击
$("html").delegate(".sc", "click", function () {
//parents()找到指定的祖先
$(this).parents(".info").remove();
});
// 创建节点方法(函数调用)
function creatE(text){
//模板字符串的使用
var $weibo = $(`<div class="info">
<p class="infoText">${text}</p>
<p class="infoOperation">
<span class="infoTime">${formartDate()}</span>
<span class="infoHandle">
<a href="javascript:;" class="top">0</a>
<a href="javascript:;"class="dowm">0</a>
<a href="javascript:;"class="sc" >删除</a>
</span>
</p>
</div>`);
return $weibo;
};
//生成时间方法
function formartDate(){
//定义了一个时间类型date 来获取当前的时间
var date=new Date();
// 2018-4-3 21:30:23
var arr=[date.getFullYear()+"-",
date.getMonth()+1+"-",
date.getDate()+" ",
date.getHours()+":",
date.getMinutes()+":",
date.getSeconds()];
//数组转换为字符串
return arr.join("");
//var str=date.getFullYear()+"-"+date.getMonth()+"-"+ date.getDay()+" "+ date.getHours()+":"+ date.getMinutes()+":"+ date.getSeconds()
};
});