1. 程式人生 > 實用技巧 >用js製作論壇發貼

用js製作論壇發貼

需求說明
單擊我要發貼,彈出發貼介面
在標題框中輸入標題,選擇所屬版塊,輸入帖子內容
單擊“釋出”按鈕,新發布的帖子顯示在列表的第一個,新帖子顯示頭像、標題、版塊和釋出時間
使用陣列儲存發帖者的頭像
使用函式floor( )和random( )隨機獲取發帖者的頭像
使用appendChild ( )把頭像、標題、版塊、時間插入到頁面中
設定value值為空來清空當前輸入框中的內容
使用style屬性隱藏發新貼介面

1.html

<!DOCTYPE html>
<html>
<head lang="en">
    <
meta charset="UTF-8"> <title>課工場論壇列表</title> <link href="css/style.css" rel="stylesheet"> </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 src="js/jquery-1.12.4.js"></script> <script src="js/luntan.js"></script> </body> </html>

js(jquery-1.12.4.js)

$(document).ready(function(){
    $(".bbs header span").click(function(){
        $(".bbs .post").show();
    });
    var tou=new Array("tou01.jpg","tou02.jpg","tou03.jpg","tou04.jpg");
    $(".post .btn").click(function(){
        var $newLi=$("<li></li>");  //建立一個新的li節點元素
        var iNum=Math.floor(Math.random()*4);  //隨機獲取頭像
        var $touImg=$("<div><img src=img/"+tou[iNum]+"></div>");  //建立頭像節點
        var $title=$("<h1>"+$(".title").val()+"</h1>"); //設定標題節點
        var newP=$("<p></p>");  //建立一個新的p節點元素
        var myDate=new Date();
        var currentDate=myDate.getFullYear()+"-"+parseInt(myDate.getMonth()+1)+"-"+myDate.getDate()+" "+myDate.getHours()+":"+myDate.getMinutes();
        $(newP).append("<span>版塊:"+$(".post select").val()+"</span>");  //在p節點中插入版塊;
        $(newP).append("<span>發表時間:"+currentDate+"</span>");     //在p節點中插入釋出時間;
        $($newLi).append($touImg);  //插入頭像
        $($newLi).append($title);   //插入標題
        $($newLi).append(newP);    //插入版塊、時間內容
        $(".bbs section ul").prepend($newLi);

        $(".post .content").val("");
        $(".post .title").val("");
        $(".post").hide();


    });
})

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;}

圖片img