新浪微博
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()
};
});