<!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>
<style>
ul,
li {
margin: 0;
padding: 0;
}
a {
color: #fff;
text-decoration: none;
}
textarea {
width: 200px;
height: 100px;
resize: none;
}
button {
width: 206px;
height: 30px;
border: none;
background-color: tomato;
outline: none;
color: #fff;
}
ul li {
width: 196px;
margin: 10px 0 0 0;
background-color: violet;
color: #fff;
position: relative;
padding: 10px 5px;
}
ul li a {
position: absolute;
right: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<textarea></textarea><br>
<button>提交留言</button>
<ul>
</ul>
<script>
var text = document.querySelector('textarea');
var btn = document.querySelector('button');
var ul = document.querySelector('ul');
btn.onclick = function() {
if (text.value == "") {
alert('请输入留言');
return false;
} else {
var li = document.createElement('li');
var a = document.createElement('a');
li.innerHTML = text.value + '<a href="javascript:(0)">删除</a>';
ul.insertBefore(li, ul.children[0]);
// li.appendChild(a);
// a.innerHTML = '删除';
var as = document.querySelectorAll('a');
for (var i = 0; i < as.length; i++) {
as[i].onclick = function() {
ul.removeChild(this.parentNode);
}
}
}
text.value = '';
}
</script>
</body>
</html>
在文本区域内输入内容点击提交留言,留言在下方显示,点击删除可将此条留言删除掉。