用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

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM