留言板功能
1.在文本框输入文字能获得元素 字符长度
2.发布按钮:点击发布
2.1文本框内容清空
2.2字符长度清空
3.删除按钮
3.1通过事件委托 子节点交给父节点处理
先找到子节点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>微博发布</title>
<style>
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
.w {
width: 900px;
margin: 0 auto;
}
.controls textarea {
width: 878px;
height: 100px;
resize: none;
border-radius: 10px;
outline: none;
padding-left: 20px;
padding-top: 10px;
font-size: 18px;
}
.controls {
overflow: hidden;
}
.controls div {
float: right;
}
.controls div span {
color: #666;
}
.controls div .useCount {
color: red;
}
.controls div button {
width: 100px;
outline: none;
border: none;
background: rgb(0, 132, 255);
height: 30px;
cursor: pointer;
color: #fff;
font: bold 14px "宋体";
transition: all 0.5s;
}
.controls div button:hover {
background: rgb(0, 225, 255);
}
.controls div button:disabled {
background: rgba(0, 225, 255, 0.5);
}
.contentList {
margin-top: 50px;
}
.contentList li {
padding: 20px 0;
border-bottom: 1px dashed #ccc;
position: relative;
}
.contentList li .info {
position: relative;
}
.contentList li .info span {
position: absolute;
top: 15px;
left: 100px;
font: bold 16px "宋体";
}
.contentList li .info p {
position: absolute;
top: 40px;
left: 100px;
color: #aaa;
font-size: 12px;
}
.contentList img {
width: 80px;
border-radius: 50%;
}
.contentList li .content {
padding-left: 100px;
color: #666;
word-break: break-all;
}
.contentList li .the_del {
position: absolute;
right: 0;
top: 0;
font-size: 28px;
cursor: pointer;
}
.delet {
height: 50px;
/* background-color:darkviolet; */
border-bottom: 2px slateblue solid;
}
.delet {
margin-top: 20px;
}
.delet button {
float: right;
width: 100px;
outline: none;
border: none;
background: rgb(0, 132, 255);
height: 30px;
cursor: pointer;
color: #fff;
font: bold 14px "宋体";
transition: all 0.5s;
}
</style>
</head>
<body>
<div class="w">
<!-- 操作的界面 -->
<div class="controls">
<img src="./images/9.6/tip.png" alt="" /><br />
<!-- maxlength 可以用来限制表单输入的内容长度 -->
<textarea placeholder="说点什么吧..." id="area" cols="30" rows="10" maxlength="200"></textarea>
<div>
<span class="useCount" id="useCount">0</span>
<span>/</span>
<span>200</span>
<button id="send">发布</button>
</div>
</div>
<!-- 批量删除 -->
<div class="delet">
<input type="checkbox" name="" id="checkAll" />
<span class="all">全选</span>
<button class="bu11">批量删除</button>
</div>
<!-- 微博内容列表 -->
<div class="contentList">
<ul id="list"></ul>
</div>
</div>
<!-- 添加了hidden属性元素会直接隐藏掉 -->
<li hidden>
<div class="info">
<input type="checkbox" class="ck" />
<img class="userpic" src="./images/9.6/03.jpg" />
<span class="username">死数据:百里守约</span>
<p class="send-time">死数据:发布于 2020年12月05日 00:07:54</p>
</div>
<div class="content">死数据:111</div>
<span class="the_del">X</span>
</li>
<script>
// maxlength 是一个表单属性, 作用是给表单设置一个最大长度
// 模拟数据: 随机用户名和头像, 页面更漂亮; 工作中: 头像和用户名来源于数据库
let dataArr = [{
uname: "司马懿",
imgSrc: "./images/9.5/01.jpg"
},
{
uname: "女娲",
imgSrc: "./images/9.5/02.jpg"
},
{
uname: "百里守约",
imgSrc: "./images/9.5/03.jpg"
},
{
uname: "亚瑟",
imgSrc: "./images/9.5/04.jpg"
},
{
uname: "虞姬",
imgSrc: "./images/9.5/05.jpg"
},
{
uname: "张良",
imgSrc: "./images/9.5/06.jpg"
},
{
uname: "安其拉",
imgSrc: "./images/9.5/07.jpg"
},
{
uname: "李白",
imgSrc: "./images/9.5/08.jpg"
},
{
uname: "阿珂",
imgSrc: "./images/9.5/09.jpg"
},
{
uname: "墨子",
imgSrc: "./images/9.5/10.jpg"
},
{
uname: "鲁班",
imgSrc: "./images/9.5/11.jpg"
},
{
uname: "嬴政",
imgSrc: "./images/9.5/12.jpg"
},
{
uname: "孙膑",
imgSrc: "./images/9.5/13.jpg"
},
{
uname: "周瑜",
imgSrc: "./images/9.5/14.jpg"
},
{
uname: "老夫子",
imgSrc: "./images/9.5/15.jpg"
},
{
uname: "狄仁杰",
imgSrc: "./images/9.5/16.jpg"
},
{
uname: "扁鹊",
imgSrc: "./images/9.5/17.jpg"
},
{
uname: "马可波罗",
imgSrc: "./images/9.5/18.jpg"
},
{
uname: "露娜",
imgSrc: "./images/9.5/19.jpg"
},
{
uname: "孙悟空",
imgSrc: "./images/9.5/20.jpg"
},
{
uname: "黄忠",
imgSrc: "./images/9.5/21.jpg"
},
{
uname: "百里玄策",
imgSrc: "./images/9.5/22.jpg"
},
];
// 补充: 如果内容是空,不让发布弹窗报错
// 创建 li 将 输入的内容 发布到 li
// 用户名和头像 随机在数组中取数据 - 索引值计算一个随机数
// 功能一: 用户输入,检测输入的字符串, 写入到span 内容 数字长度
let area = document.querySelector("#area"); //文本框
let useCount = document.querySelector(".useCount"); //数字长度
// // 功能二: 获取 字符 的长度
area.addEventListener("input", function () {
useCount.innerHTML = area.value.length
})
// 发布按钮 点击
/*1.将内容显示在页面
*2文本框 清零
3.文字长度清零
*/
let btn = document.querySelector("#send")
let list = document.querySelector("#list")
btn.addEventListener("click", function () {
// if 判断 如果 文本框 为空 弹窗
if (area.value == 0) {
alert("请输入内容!")
return
}
let newLi = document.createElement("li");
// 用户的头像和名字是随机的 所以 生成一组随机数
let num = Math.floor(Math.random() * dataArr.length)
newLi.innerHTML = `
<div class="info">
<input type="checkbox" class="ck" />
<img class="userpic" src="${dataArr[num].imgSrc}" />
<span class="username">${dataArr[num].uname}</span>
<p class="send-time">${new Date().toLocaleString()}</p>
</div>
<div class="content">${area.value}</div>
<span class="the_del">X</span>
`;
// 把新的数据 插在 list 后面
list.insertBefore(newLi, list.children[0]);
// 文本字长度清零 文本框内容清零
// 创建 一个li 将为本内容插进去
area.value = ""
useCount.innerHTML = 0
// 删除留言
// 事件委托 通过找儿子 委托给他他的 爹来执行处理
list.addEventListener("click", function (e) {
if(e.target.classList.contains("the_del")){
list.removeChild(e.target.parentNode)
}
})
})
// 全选功能
let checkAll = document.querySelector("#checkAll")
let text = document.querySelector(".all")
checkAll.addEventListener("change", function () {
// 遍历
let cks = document.querySelectorAll(".ck")
for (let i = 0; i < cks.length; i++) {
cks[i].checked = checkAll.checked
}
console.log(111);
if (checkAll.checked === true) {
text.innerHTML = "取消";
} else {
text.innerHTML = "全选";
}
});
// 全部删除
let bu11 = document.querySelector(".bu11")
bu11.addEventListener("click", function () {
list.innerHTML = ""
})
</script>
</body>
</html>
结果如下: