在现代网页设计中,留言板是一个非常实用的功能,它能够为用户提供一个便捷的方式来发表意见或留言。下面是一段简洁且易于实现的留言板HTML代码示例。这段代码不仅结构清晰,而且功能完整,适合初学者快速上手。
```html
body {
font-family: Arial, sans-serif;
background-color: f4f4f9;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.message-board {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
width: 350px;
}
textarea {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid ccc;
border-radius: 4px;
resize: none;
}
button {
width: 100%;
padding: 10px;
background-color: 007BFF;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: 0056b3;
}
ul {
list-style-type: none;
padding: 0;
}
li {
background-color: f9f9f9;
margin-bottom: 5px;
padding: 10px;
border-radius: 4px;
}
<script>
function addMessage() {
const messageInput = document.getElementById('messageInput');
const messagesList = document.getElementById('messages');
if (messageInput.value.trim() === '') return;
// 创建新的列表项
const newMessage = document.createElement('li');
newMessage.textContent = messageInput.value;
messagesList.appendChild(newMessage);
// 清空输入框
messageInput.value = '';
}
</script>
```
功能说明:
1. 界面设计:使用简单的CSS样式来确保页面看起来干净整洁,同时提供良好的用户体验。
2. 交互功能:用户可以在文本区域输入留言,并通过点击“提交留言”按钮将其添加到留言板下方的列表中。
3. 动态更新:每次提交留言后,无需刷新页面即可看到最新的留言内容。
这段代码非常适合用来学习HTML和JavaScript的基础知识,同时也展示了如何结合前端技术构建一个小型但实用的应用程序。希望这个例子对你有所帮助!