<!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>todoList:留言板</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
div{
width: 62.5rem;
height: auto;
margin: 0 auto;
margin-top: 50px;
}
input{
border: 1px solid salmon;
height: 2rem;
width: 20rem;
outline: none;
}
input[type=text]::placeholder {
color: blue;
padding-left:1rem;
}
.list li{
margin-top: 1rem;
list-style-type:none
}
.list li > button{
border-radius:0.5rem;
width:2.5rem;
height: 2rem;
outline: none;
margin-left: 1rem;
background-color: bisque;
border: none;
}
</style>
</head>
<body>
<div>
<input type="text" onkeydown="insertComment(this)" placeholder="请输入留言" autofocus>
<ul class="list">
</ul>
</div>
<script>
let patt = /[\u4E00-\u9FA5]/;
const insertComment = ((ele)=>{
if(event.key === ‘Enter’){
if(ele.value.length === 0){
alert(‘留言不能为空’);
ele.focus();
//直接返回
return false;
}
if(!ele.value.match(patt)){
alert(‘必须是汉字’);
ele.focus();
//直接返回
ele.value = null;
return false;
}
const ul = document.querySelector(‘.list’);
ele.value +=<button onclick="del(this.parentNode)">删除</button>
ul.insertAdjacentHTML(‘afterbegin’,<li>${ele.value}</li>
)
ele.value = null;
}
})
const del = function(ele){
return confirm(‘是否删除’)?(ele.outerHTML = null) :false;
}
</script>
</body>
</html>