首页 技术 正文
技术 2022年11月7日
0 收藏 692 点赞 944 浏览 2781 个字

  原生JS实现简单留言板功能,实现技术:css flex,原生JS。

  因为主要是为了练手js,所以其中布局上的一些细节并未做处理。

  

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>原生JS 实现留言板功能</title>
<style>
* {
padding: 0;
margin: 0;
}
.container {
width: 1000px;
max-height: 100vh;
background: lightgray;
margin: 0 auto;
}
.container .content {
padding: 20px 20px 0 20px;
display: flex;
flex-direction: column;
}
.container .message {
display: flex;
flex-direction: row;
justify-content: space-between;
width: 100%;
height: 200px;
align-items: flex-end;
}
.container .enterMsg {
width: 76%;
height: 180px;
font-size: 18px;
padding: 10px 2%;
line-height: 26px;
}
.container .sendMsg {
width: 15%;
height: 60px;
line-height: 60px;
display: inline-block;
font-size: 18px;
cursor: pointer;
text-align: center;
background: lightcoral;
border-radius: 6px;
}
.container .msgs {
margin-top: 20px;
height: calc(100vh - 270px);
background: lightyellow;
overflow: auto;
border-radius: 6px;
padding: 10px 2%;
}
.container .msgs h3 {
margin-bottom: 25px;
}
.container .msgList {
max-height: calc(100vh - 380px);
overflow: auto;
}
.container .singleMsg {
border-bottom: thin solid #ccc;
padding: 15px 0 15px 0;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.container .singleMsg .delete {
color: #999;
cursor: pointer;
}
.container .singleMsg .delete:hover {
text-decoration: underline;
}
.container .total {
text-align: right;
height: 60px;
line-height: 60px;
}
</style>
</head>
<body><div class="container">
<div class="content">
<div class="message">
<textarea name="msg" id="enterMsg" class="enterMsg" placeholder="请开始你的表演吧......"></textarea>
<span id="sendMsg" class="sendMsg">留 言</span>
</div>
<div class="msgs">
<h3>留言区</h3>
<div class="msgList" id="messageList">
<div class="singleMsg">
<p>一个人静静坐在电脑前写代码,有种武林高手闭关修炼的感觉!</p>
<p class="delete">删除</p>
</div>
</div>
<div class="total">总共有 <span id="votes">1</span>条留言</div>
</div>
</div>
</div><script>
//设置页面高度,保持一屏显示
var maxH = document.documentElement.clientHeight;
var container = document.querySelector('.container');
container.style.height = maxH + 'px'; //留言
var enterMsg = document.getElementById('enterMsg');
var sendMsg = document.getElementById('sendMsg');
var msgList = document.getElementById('messageList');
var votes = 0; //统计留言条数 sendMsg.onclick = function(e){
var msg = enterMsg.value;
if(msg === ''){
alert('您还没有输入内容哦!');
return;
}
new createDiv(msg, msgList);
votes ++;
enterMsg.value = '';
//msg = ''; //把enterMsg的value值保存下来后,msg只是一个副本,与enterMsg没有关系
document.getElementById('votes').innerHTML = votes;
}; function createDiv(obj, el){
this.div = document.createElement('div');
this.div.className = 'singleMsg';
this.p1 = document.createElement('p');
this.p1.innerHTML = obj;
this.p2 = document.createElement('p');
this.p2.className = 'delete';
this.p2.innerHTML = '删除';
this.div.appendChild(this.p1);
this.div.appendChild(this.p2);
el.insertBefore(this.div, el.childNodes[0]); var that = this;
this.p2.onclick = function(){
that.div.remove();
if(votes <= 0){
votes = 0;
}
votes --;
document.getElementById('votes').innerHTML = votes;
};
}
</script></body>
</html>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,356
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,139
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848