效果展示
![在这里插入图片描述](https://img-blog.csdnimg.cn/e59b7f79d2214215bb3fc4ecc2d66c35.png)
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
li {
width: 200px;
height: 25px;
line-height: 25px;
margin: 2px 0;
background-color: rgb(188, 240, 222);
}
a {
text-decoration: none;
}
ul li a {
float: right;
}
</style>
</head>
<body>
<textarea name="input" id="text" cols="30" rows="10" style="resize: none;" placeholder="please input"></textarea>
<button class="btn">留言</button>
<ul></ul>
<script>
const btn=document.getElementsByClassName("btn")
const ul=document.querySelector('ul');
btn[0].onclick=function() {
const text=document.getElementById("text");
if(text.value=='') {
alert("please input");
return false;
}else {
const li=document.createElement("li");
li.innerHTML=text.value+"<a href='javascript:;'>删除</a>";
ul.appendChild(li);
const list=document.querySelectorAll("a");
for(let i=0;i<list.length;i++) {
list[i].onclick=function() {
ul.removeChild(this.parentNode);
}
}
}
text.value='';
}
</script>
</body>
</html>