li.innerHTML
赋值给liappendChild
,如果想要前面显示就用insertBefore
<!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 {
list-style: none;
margin-bottom: 5px;
background-color: #ddffff !important;
padding: 14px;
border-left: 6px solid #ccc !important;
border-color: #2196F3 !important;
}
textarea {
border: 0;
border-radius: 5px;
background-color: rgba(241, 241, 241, .98);
width: 355px;
height: 100px;
padding: 10px;
resize: none;
}
button {
width: 300px;
background-color: #4CAF50;
/* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
-webkit-transition-duration: 0.4s;
/* Safari */
transition-duration: 0.4s;
border-radius: 10px;
}
</style>
</head>
<body style="text-align: center">
<textarea name="" id="" cols="50" rows="10" placeholder="在这里写下你的留言吧"></textarea>
<div> <button>立即发布</button></div>
<ul style="text-align: left">
</ul>
<script>
var btn = document.querySelector('button');
var text = document.querySelector('textarea');
var ul = document.querySelector('ul');
btn.onclick = function () {
if (text.value == '') {
alert('您没有输入内容')
return false;
} else {
var li = document.createElement('li');
li.innerHTML = text.value;
//ul.appendChild(li); 后面插入
ul.insertBefore(li, ul.children[0]);
}
}
</script>
</html>