Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:可以, 效果实现的不错, 可以再尝试用css美化一下
<!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>留言板案例</title>
</head>
<body>
<style>
div {
width: 300px;
height: 500px;
border: 3px solid black;
display: grid;
grid-template-rows: 1fr 30px;
background-color: green;
}
div > input{
background-color: rgb(71, 37, 174);
}
li {
list-style: none;
border: 1px solid red;
margin: 5px;
}
</style>
<div>
<ul class="test">
</ul>
<input type="text" onkeydown="insertComment(this)" placeholder="请输入留言" autofocus>
</div>
<script>
const insertComment = function(ele){
// 按下回车键提交
if(event.key === 'Enter'){
// 非空判断
if(ele.value.length === 0){
alert('消息不能为空')
// 重置焦点
ele.focus()
// 直接返回
return false
}
const ul = document.querySelector('.test');
ul.insertAdjacentHTML('beforeend', `<li>${ele.value}</li>`);
ele.value = null
}
}
</script>
</body>
</html>