本文实例为大家分享了JavaScript实现留言板的具体代码,供大家参考,具体内容如下
效果:
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
<!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 > 说点什么吗...... </ 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 >个字</ span > < input type = "button" value = "发布" id = "btn" /> </ div > < div > < div >大家在说</ 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);//追加节点 box.insertBefore(divItem, box.firstChild);//插入节点 var div1 = document.createElement("div"); div1.className = "c1"; var div2 = document.createElement("div"); divItem.appendChild(div1); divItem.appendChild(div2); //设置第1个div的内容 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; } //封装一个日期格式化的函数 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 > |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/dongdongaa0/article/details/112341029