目录
需求:1.点击输入框时,下方显示输入框文本长度
2.失去焦点时,输入框文本长度消失
3.发布每条评论时按时间顺序排序
4.可以实时删除评论
css代码
<style>
* {
padding: 0;
margin: 0;
}
.wrapper {
width: 500px;
}
input {
height: 60px;
padding-left: 20px;
width: 500px;
outline: none;
border: 1px solid #9c9b9c;
border-radius: 10px;
}
/* input:focus {
height: 60px;
background-color: white;
} */
.top p {
text-align: right;
margin-top: 3px;
}
button {
width: 100px;
height: 30px;
color: white;
background-color: #3686ff;
outline: none;
border: none;
}
.list .img {
width: 60px;
height: 60px;
border-radius: 50%;
background-color: darkorange;
margin-right: 20px;
}
.list {
display: flex;
position: relative;
height: 100px;
border-bottom: 1px dashed #adabab;
padding: 10px;
}
.list span {
position: absolute;
display: block;
width: 20px;
height: 20px;
/* background-color: aqua; */
cursor: pointer;
top: 0;
right: 0;
font-size: 20px;
text-align: center;
line-height: 20px;
}
.info .name {
font-weight: 600;
}
.info .time {
color: #c9cdd5;
font-size: 14px;
margin-bottom: 20px;
margin-top: 10px;
}
.info .text {
color: #757575;
}
.top .publish {
opacity: 0;
transition: all 0.5s;
}
ul {
margin-top: 30px;
}
</style>
html代码
<div class="wrapper">
<div class="top">
<input type="text" placeholder="说点什么吧...">
<p> <span class="publish">0/200字</span><button>发布</button></p>
</div>
<ul>
<li>
<!-- <div class="list">
<div class="img"></div>
<div class="info">
<p class="name">孙膑</p>
<p class="time">发布于</p>
<p class="text">今天吃大肘子</p>
<span>x</span>
</div>
</div>
-->
</li>
</ul>
</div>
js代码
思路:先给input输入框添加获取焦点时,显示文本长度
失去焦点时,隐藏文本长度
给input框添加inpu事件,input事件是跟随input框内容的变化而变化,因而可以实时获取输 入框的内容长度
再给按钮添加点击事件,并动态创建li来存储评论内容
封装一个获取当前时间的函数
给创建的li中的某个元素添加点击事件,用于删除评论
<script>
const btn = document.querySelector('button')
const ipt = document.querySelector('input')
const span = document.querySelector('.top span')
const pub = document.querySelector('.top p .publish')
//获取焦点显示
ipt.addEventListener('focus', function () {
pub.style.opacity = 1
})
//失去焦点隐藏
ipt.addEventListener('blur', function () {
pub.style.opacity = 0
})
ipt.addEventListener('input', function () {
pub.innerHTML = `${ipt.value.length}/200字`
})
//给按钮添加事件
const ul = document.querySelector('ul')
btn.addEventListener('click', function () {
//判断输入框是是否为空
if (ipt.value === '') {
alert('请输入内容')
return
} else {
//创建li
let li = document.createElement('li')
li.innerHTML = `<div class="list">
<div class="img"></div>
<div class="info">
<p class="name">孙膑</p>
<p class="time">发布于${getTime()}</p>
<p class="text">${ipt.value}</p>
<span>x</span>
</div>
</div>`
ul.prepend(li)
ipt.value = ''
}
//给span绑定事件
let spans = document.querySelectorAll('.info span')
for (let i = 0; i < spans.length; i++) {
spans[i].addEventListener('click', function () {
this.parentNode.parentNode.parentNode.remove()
// ul.removeChild(this.parentNode.parentNode.parentNode)
})
}
})
//获取当前时间
function getTime() {
let date = new Date()
let y = date.getFullYear()
let m = date.getMonth() + 1
m = m < 10 ? '0' + m : m
let d = date.getDate()
d = d < 10 ? '0' + d : d
let h = date.getHours()
h = h < 10 ? '0' + h : h
let mm = date.getMinutes()
mm = mm < 10 ? '0' + mm : mm
let s = date.getSeconds()
s = s < 10 ? '0' + s : s
return `${y}/${m}/${d} ${h}:${mm}:${s}`
}
</script>
完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
.wrapper {
width: 500px;
}
input {
height: 60px;
padding-left: 20px;
width: 500px;
outline: none;
border: 1px solid #9c9b9c;
border-radius: 10px;
}
/* input:focus {
height: 60px;
background-color: white;
} */
.top p {
text-align: right;
margin-top: 3px;
}
button {
width: 100px;
height: 30px;
color: white;
background-color: #3686ff;
outline: none;
border: none;
}
.list .img {
width: 60px;
height: 60px;
border-radius: 50%;
background-color: darkorange;
margin-right: 20px;
}
.list {
display: flex;
position: relative;
height: 100px;
border-bottom: 1px dashed #adabab;
padding: 10px;
}
.list span {
position: absolute;
display: block;
width: 20px;
height: 20px;
/* background-color: aqua; */
cursor: pointer;
top: 0;
right: 0;
font-size: 20px;
text-align: center;
line-height: 20px;
}
.info .name {
font-weight: 600;
}
.info .time {
color: #c9cdd5;
font-size: 14px;
margin-bottom: 20px;
margin-top: 10px;
}
.info .text {
color: #757575;
}
.top .publish {
opacity: 0;
transition: all 0.5s;
}
ul {
margin-top: 30px;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="top">
<input type="text" placeholder="说点什么吧...">
<p> <span class="publish">0/200字</span><button>发布</button></p>
</div>
<ul>
<li>
<!-- <div class="list">
<div class="img"></div>
<div class="info">
<p class="name">孙膑</p>
<p class="time">发布于</p>
<p class="text">今天吃大肘子</p>
<span>x</span>
</div>
</div>
-->
</li>
</ul>
</div>
<script>
const btn = document.querySelector('button')
const ipt = document.querySelector('input')
const span = document.querySelector('.top span')
const pub = document.querySelector('.top p .publish')
//获取焦点显示
ipt.addEventListener('focus', function () {
pub.style.opacity = 1
})
//失去焦点隐藏
ipt.addEventListener('blur', function () {
pub.style.opacity = 0
})
ipt.addEventListener('input', function () {
pub.innerHTML = `${ipt.value.length}/200字`
})
//给按钮添加事件
const ul = document.querySelector('ul')
btn.addEventListener('click', function () {
//判断输入框是是否为空
if (ipt.value === '') {
alert('请输入内容')
return
} else {
//创建li
let li = document.createElement('li')
li.innerHTML = `<div class="list">
<div class="img"></div>
<div class="info">
<p class="name">孙膑</p>
<p class="time">发布于${getTime()}</p>
<p class="text">${ipt.value}</p>
<span>x</span>
</div>
</div>`
ul.prepend(li)
ipt.value = ''
}
//给span绑定事件
let spans = document.querySelectorAll('.info span')
for (let i = 0; i < spans.length; i++) {
spans[i].addEventListener('click', function () {
this.parentNode.parentNode.parentNode.remove()
// ul.removeChild(this.parentNode.parentNode.parentNode)
})
}
})
//获取当前时间
function getTime() {
let date = new Date()
let y = date.getFullYear()
let m = date.getMonth() + 1
m = m < 10 ? '0' + m : m
let d = date.getDate()
d = d < 10 ? '0' + d : d
let h = date.getHours()
h = h < 10 ? '0' + h : h
let mm = date.getMinutes()
mm = mm < 10 ? '0' + mm : mm
let s = date.getSeconds()
s = s < 10 ? '0' + s : s
return `${y}/${m}/${d} ${h}:${mm}:${s}`
}
</script>
</body>
</html>