<!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>Document</title>
<link rel="stylesheet" href="./1.css">
<script src="./1.js"></script>
</head>
<body>
<div class="w">
<div class="controls">
<img src="images/tip.png" alt=""><br>
<textarea placeholder="输入你想说的话" id="area" cols="30" rows="10" maxlength="200"></textarea>
<div>
<span class="useCount">0</span>
<span>/</span>
<span>200</span>
<button id="send">发送</button>
</div>
</div>
<div class="contentList">
<ul id="list">
</ul>
</div>
</div>
<script>
const txt = document.querySelector("#area")
const ch_span = document.querySelector(".useCount")
const btn = document.querySelector("#send")
const ul = document.querySelector(".contentList ul")
txt.addEventListener("input",function(){
console.log(txt.value.length)
ch_span.innerHTML = txt.value.length
})
btn.addEventListener("click",function(){
if(txt.value.trim().length>0){
let newLi = document.createElement("li")
newLi.innerHTML = `${txt.value.trim()} <button>删除</button>`
ul.insertBefore(newLi,ul.children[0])
ch_span.innerHTML = "0"
txt.value = ""
}else{
alert("请输入内容!")
}
})
txt.addEventListener("keydown",function(e){
if(e.key==="Enter"){
btn.click()
}
})
ul.addEventListener("click",function(e){
if(e.target.tarName === "BUTTON"){
ul.removeChild(e.target.parentNode)
}
})
</script>
</body>
</html>
css:
* {
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:pink;
height: 30px;
cursor: pointer;
color: #fff;
font: bold 14px '宋体';
transition: all 0.5s;
}
.controls div button:hover {
background:yellow;
}
.controls div button:disabled {
background:aqua;
}
.contentList {
margin-top: 50px;
}
.contentList ul {
height: 800px;
}
.contentList li {
/* display: none; */
padding: 20px 0;
border-bottom: 1px dashed #ccc;
height: 60px;
}
.contentList li a {
margin-left: 800px;
}