一.CSS美化留言板
1.输出结果
2.代码部分
<!DOCTYPE html>
<html lang="zh-CN">
<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>CSS美化留言板</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: lightblue;
max-height: 1120em;
max-width: 1170px;
margin: 0 auto;
padding: 0 30px;
}
h3 {
margin: 20px 10px;
text-align: center;
}
input {
display: block;
width: 100%;
min-height: 200px;
margin-bottom: 20px;
padding: 5px 10px 250px;
}
.list > li {
display: flex;
place-content: space-between;
background-color: lightcoral;
line-height: 36px;
margin-bottom: 5px;
border-radius: 3px;
padding: 0 10px;
}
.list > li > button {
border-radius: 5px;
margin: 5px 0;
border: 1px;
width: 40px;
}
</style>
</head>
<body>
<h3>留言板</h3>
<input type="text" onkeydown="insertComment(this)" placeholder="请输入您的留言" autofocus/>
<ul class="list"></ul>
</body>
<script>
const insertComment = function (ele) {
if (event.key === 'Enter') {
// 1. 非空判断 event指事件对象
if (ele.value.length === 0) {
alert('留言不能为空')
// 重置焦点
ele.focus();
// 直接返回
return false;
}
// 2. 添加留言
const ul = document.querySelector('.list');
ele.value += `<button onclick="del(this.parentNode)">删除</button>`;
ul.insertAdjacentHTML('afterbegin', `<li>${ele.value}</li>`);
// 3. 清空输入框
ele.value = null;
}
};
// 删除
const del = function(ele) {
return confirm('是否删除?') ? (ele.outerHTML = null) : false;
};
</script>
</html>