Delete Message

The core idea: After clicking the button, you create a dynamic li, ul added to the inside;

Li created at the same time, the text field inside the values ​​assigned to li.innerHTML li;

If you want to display a new message on the back with appendChild If you want to display on the front with insertBefore;

 
<!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>
</head>

<body>
<textarea name="" id="" cols="30" rows="10"></textarea>
<button>按钮</button>
<ul></ul>
<script>
var txt = document.querySelector('textarea');
var btn = document.querySelector('button');
var = document.querySelector site ( "site");
btn.onclick = function() {
if (txt.value == '') {
alert ( 'not empty');
return false;
} else {
var li = document.createElement('li');
li.innerHTML = txt.value + '<a href="javascript:;">删除</a>';
ul.insertBefore(li, ul.children[0]);
var as = document.querySelectorAll('a');
for (var j = 0; j < as.length; j++) {
as[j].onclick = function() {
ul.removeChild(this.parentNode)
}
}
}
}
</script>
</body>

</html>

Guess you like

Origin www.cnblogs.com/lh1998/p/11454928.html