您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)使用原生JavaScript編寫(xiě)一個(gè)留言板功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> #txt2{ width:400px; height:50px; margin-top:5px; } #span1{ margin-left:200px; } #box{ width:400px; /*height:400px;*/ /*border:1px solid gray;*/ } #box .item{ height:80px; border-bottom:dashed 1px lightgrey } #box .c1{ height:30px; } #box .c1 span{ float:left; } #box .c1 a{ float:right; margin-right:20px; text-decoration: none; color:black; } </style> </head> <body> <div> 說(shuō)點(diǎn)什么嗎...... </div> <div> <input type="text" placeholder="洪七公" id="txt1"/> </div> <div> <textarea name="" id="txt2" maxlength="10"></textarea> </div> <div id="div3"> <span id="span1">還能輸入<span id="span2">10</span>個(gè)字</span> <input type="button" value = "發(fā)布" id="btn"/> </div> <div> <div>大家在說(shuō)</div> </div> <div id="box"> <div class = "item"> <div class = "c1"> <span>喬峰:</span> <span>今天............</span> <a href = "#">刪除</a> </div> <div>01月04日 01:28</div> </div> <div class = "item"> <div class = "c1"> <span>喬峰:</span> <span>今天............</span> <a href = "#">刪除</a> </div> <div>01月04日 01:28</div> </div> </div> </body> <script type="text/javascript"> //獲取按鈕 var btn = document.getElementById("btn"); var box = document.getElementById("box"); var txt1 = document.getElementById("txt1"); var txt2 = document.getElementById("txt2"); btn.onclick = function () { //console.log(this); var divItem = document.createElement("div"); divItem.className = "item"; //box.appendChild(divItem);//追加節(jié)點(diǎn) box.insertBefore(divItem, box.firstChild);//插入節(jié)點(diǎn) var div1 = document.createElement("div"); div1.className = "c1"; var div2 = document.createElement("div"); divItem.appendChild(div1); divItem.appendChild(div2); //設(shè)置第1個(gè)div的內(nèi)容 var span1 = document.createElement("span"); div1.appendChild(span1); span1.innerHTML = txt1.value + " : "; var span2 = document.createElement("span"); div1.appendChild(span2); span2.innerHTML = txt2.value; var a = document.createElement("a"); a.href = "#"; a.innerHTML = "刪除"; div1.appendChild(a); //a的刪除事件 a.onclick = function () { this.parentNode.parentNode.remove(); } var date = new Date(); //var str = date.toLocaleString(); var m = date.getMonth() + 1; var d = date.getDate(); var h = date.getHours(); var m2 = date.getMinutes(); m = fun1(m); d = fun1(d); h = fun1(h); m2 = fun1(m2); var str = m + "月" + d + "日 " + h + ":" + m2; div2.innerHTML = str; } //封裝一個(gè)日期格式化的函數(shù) function fun1(x) { if (x < 10) { return "0" + x; } return x; } var span2 = document.getElementById("span2"); txt2.onkeyup = function () { var str = this.value; console.log(str.length); span2.innerHTML = 10 - str.length; } </script> </html>
關(guān)于使用原生JavaScript編寫(xiě)一個(gè)留言板功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。