原生js模仿QQ空間留言版

代碼很簡(jiǎn)單,主要是利用createElement、appendChild、removeChild來(lái)創(chuàng)建、添加和刪除元素。使用textarea標(biāo)簽作為輸入框,button提交。表單提交后還得刷新頁(yè)面,所以就沒有使用。剛學(xué)習(xí)html dom可能有些地方寫得很蠢,歡迎批評(píng)指正。github地址:https://github.com/llycc/learn-js。以下是代碼。

<!DOCTYPE html>
<html>
    <head>
        <title>Message Board</title>
        <style type="text/css">
            .container {
                width:60%;
                margin:0 auto;
                padding:0 0 50px 0;
                border:1px solid rgba(51,204,255,0.5);
            }
            .container > div {
                margin: 0 auto;
                width:90%;
            }
            .inputMeg > textarea {
                width:100%;
                height:150px;
                resize:none;
            }
            .inputMeg button {
                width:70px;
                height:30px;
                color:white;
                font-size:15px;
                background-color:#6acdea;
                border:1px solid #72c1d8;
                border-radius:7%;
                cursor:pointer;
                display:block;
            }
            .showMeg {
                padding:10px 0;
                border-top:1px solid rgba(51,204,255,1);
            }
            .showMeg > div {
                margin-bottom:10px;
                border-bottom:1px solid rgba(51,204,255,0.3);
            }
        </style>
    </head>
    
    <body>
        <div class="container">
            <div>
                <div class="inputMeg">
                    <p>發(fā)表您的留言</p>
                    <textarea name="message" placeholder="一聲留言 十年思念" autofocus></textarea>
                    <button type="button" id="pubMeg">發(fā)表</button>
                </div>
                <p>留言</p>
                <div class="showMeg">
                </div>
            </div>
        </div>
        <script type="text/javascript">
            var index = 1;
            var message = document.getElementsByName("message").item(0);
            
            (function pubMessage(){
                var pubMeg = document.getElementById("pubMeg");
                var showMeg = document.querySelector(".showMeg");
                
                pubMeg.onclick = function(){
                    
                    var text = message.value;
                    //非空檢查
                    if(text.search(/[^\r\n\s]/) === -1){
                        alert("您還沒有填寫留言");
                        return;
                    }
                    var megBox = document.createElement("div"),
                        megHead = document.createElement("span"),
                        megText = document.createElement("p"),
                        option = document.createElement("select"),
                        reply = document.createElement("button"),
                        replyInput = document.createElement("textarea"),
                        showReply = document.createElement("div");
                                                        
                    //樓層數(shù)和日期
                    var now = new Date();
                    var time = now.toTimeString().split(" ")[0];
                    megHead.style.color = "#b0b0b0";
                    megHead.innerHTML = `#${index++} ${now.getUTCFullYear()}-${now.getMonth() + 1}-${now.getDate()} ${time}`;
                    
                    megText.innerHTML = text.replace(/\r/ig,"").replace(/\n/ig,"<br/>");
                    //操作選項(xiàng)
                    option.innerHTML = "<option value=\"option\">操作</option><option value=\"del\">刪除</option>"
                    option.style.float = "right";
                    
                    option.onchange = function(){
                        var selected = this.value;
                        this.selectedIndex = 0;
                        if(selected === "del"){
                            let res = confirm("刪除留言后不可恢復(fù),您確定要繼續(xù)嗎?");
                            if(res){
                                this.parentNode.parentNode.removeChild(this.parentNode);
                            }
                        }else{
                            
                        }
                    }
                    //回復(fù)按鈕
                    reply.innerHTML = "回復(fù)";
                    reply.type = "button";
                    reply.style.cursor = "pointer";
                    reply.style.marginBottom = "20px";
                    reply.style.fontSize = "10px";
                    reply.style.color = "white";
                    reply.style.backgroundColor = "#6acdea"
                    reply.style.border = "1px solid #72c1d8";
                    reply.style.borderRadius="7%";
                    reply.style.display = "block";
                    
                    reply.onclick = function (){
                        let replyMeg = replyInput.value;
                        if(replyMeg.search(/[^\r\n\s]/) === -1){
                            alert("請(qǐng)輸入回復(fù)內(nèi)容");
                            return;
                        }
                    
                        let div = document.createElement("div");
                            messageP = document.createElement("p"),
                            timeSpan = document.createElement("span"),
                            delSpan = document.createElement("span");
                        
                        div.style.margin = "10px 0";
                        messageP.style.margin = "0";
                        messageP.style.fontSize = "13px";
                        
                        timeSpan.style.fontSize = "13px";
                        
                        //刪除回復(fù)按鈕
                        delSpan.innerHTML = "刪除";
                        delSpan.style.color = "blue";
                        delSpan.style.fontSize = "13px";
                        delSpan.style.cursor = "pointer";
                        delSpan.style.marginLeft = "10px";
                        
                        delSpan.onclick = function(){
                            let res = confirm("您確定要?jiǎng)h除此條回復(fù)嗎?");
                            if(res){
                                showReply.removeChild(div);
                            }
                        }
                        
                        //回復(fù)時(shí)間
                        let now = new Date();
                        let time = now.toTimeString().split(" ")[0];
                        timeSpan.style.color = "#b0b0b0";
                        timeSpan.innerHTML = `${now.getUTCFullYear()}-${now.getMonth() + 1}-${now.getDate()} ${time}`;
                        
                        messageP.innerHTML = "我:" + replyMeg.replace(/\r/ig,"").replace(/\n/ig,"<br/>");
                        
                        div.appendChild(messageP);
                        div.appendChild(timeSpan);
                        div.appendChild(delSpan);
                        showReply.appendChild(div);
                        replyInput.value = null;
                    }
                    
                    //回復(fù)輸入框
                    replyInput.style.width = "50%";
                    replyInput.style.height = "20px";
                    replyInput.placeholder = "我也說(shuō)一句...";
                    
                    showReply.style.marginLeft = "20px";
                    
                    megBox.appendChild(megHead);
                    megBox.appendChild(option);
                    megBox.appendChild(megText);
                    megBox.appendChild(showReply);
                    megBox.appendChild(replyInput);
                    megBox.appendChild(reply);
                    
                    showMeg.insertBefore(megBox,showMeg.firstChild);
                    message.value = null;
                };
                
            })();
        </script>
    </body>

</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 表單基礎(chǔ)知識(shí) 在HTML中,表單是由 元素來(lái)表示的,而在JS中,表單對(duì)應(yīng)的則是HTMLFormElement類型。...
    oWSQo閱讀 977評(píng)論 0 1
  • 工廠模式類似于現(xiàn)實(shí)生活中的工廠可以產(chǎn)生大量相似的商品,去做同樣的事情,實(shí)現(xiàn)同樣的效果;這時(shí)候需要使用工廠模式。簡(jiǎn)單...
    舟漁行舟閱讀 8,131評(píng)論 2 17
  • 單例模式 適用場(chǎng)景:可能會(huì)在場(chǎng)景中使用到對(duì)象,但只有一個(gè)實(shí)例,加載時(shí)并不主動(dòng)創(chuàng)建,需要時(shí)才創(chuàng)建 最常見的單例模式,...
    Obeing閱讀 2,320評(píng)論 1 10
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,228評(píng)論 25 708
  • WOW64簡(jiǎn)介 WOW64(Windows 32-bit On Windows 64-bit)是x64平臺(tái)上運(yùn)行w...
    小豬啊嗚閱讀 3,335評(píng)論 0 6

友情鏈接更多精彩內(nèi)容