html用js制作简易留言板,首页包含留言的表单,提交后即可在后台数据中添加留言记录,留言留言内容每页显示三个,分页形式采用第一页,下一页,最后一页,删除留言功能,删除时要求输入密码(密码无需存入数据库)在留言
时间: 2023-08-08 17:13:22 浏览: 148
板中实现。可以使用 localstorage 来模拟数据库进行数据存储。
HTML 页面部分:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>留言板</title>
</head>
<body>
<h1>留言板</h1>
<form>
<label for="name">姓名:</label>
<input type="text" id="name" name="name"><br>
<label for="content">留言内容:</label>
<textarea id="content" name="content"></textarea><br>
<button type="button" id="submit">提交</button>
</form>
<div id="message-list"></div>
<div id="pagination"></div>
<script src="message-board.js"></script>
</body>
</html>
```
JS 部分:
```javascript
// 每页显示的留言数量
const PAGE_SIZE = 3;
// 最大页数
let maxPage = 0;
// 当前页数
let currentPage = 0;
// 留言数据
let messages = [];
// 将留言数据保存到本地存储
function saveMessages() {
localStorage.setItem('messages', JSON.stringify(messages));
}
// 从本地存储中获取留言数据
function loadMessages() {
const data = localStorage.getItem('messages');
if (data) {
messages = JSON.parse(data);
}
}
// 添加一条留言
function addMessage(name, content) {
const message = {
name: name,
content: content,
time: new Date().toLocaleString()
};
messages.push(message);
saveMessages();
}
// 删除一条留言
function deleteMessage(index, password) {
if (password === '123456') {
messages.splice(index, 1);
saveMessages();
return true;
} else {
return false;
}
}
// 显示留言列表
function showMessages() {
const messageList = document.getElementById('message-list');
messageList.innerHTML = '';
const startIndex = currentPage * PAGE_SIZE;
const endIndex = Math.min(startIndex + PAGE_SIZE, messages.length);
for (let i = startIndex; i < endIndex; i++) {
const message = messages[i];
const div = document.createElement('div');
div.innerHTML = `
<p><strong>${message.name}</strong> 于 ${message.time} 发表:</p>
<p>${message.content}</p>
<button type="button" class="delete" data-index="${i}">删除</button>
`;
messageList.appendChild(div);
}
}
// 显示分页器
function showPagination() {
const pagination = document.getElementById('pagination');
pagination.innerHTML = '';
if (maxPage > 1) {
const prevButton = document.createElement('button');
prevButton.innerText = '上一页';
prevButton.disabled = currentPage === 0;
prevButton.addEventListener('click', () => {
currentPage--;
showMessages();
showPagination();
});
pagination.appendChild(prevButton);
for (let i = 0; i < maxPage; i++) {
const button = document.createElement('button');
button.innerText = i + 1;
button.disabled = i === currentPage;
button.addEventListener('click', () => {
currentPage = i;
showMessages();
showPagination();
});
pagination.appendChild(button);
}
const nextButton = document.createElement('button');
nextButton.innerText = '下一页';
nextButton.disabled = currentPage === maxPage - 1;
nextButton.addEventListener('click', () => {
currentPage++;
showMessages();
showPagination();
});
pagination.appendChild(nextButton);
}
}
// 初始化留言板
function init() {
loadMessages();
maxPage = Math.ceil(messages.length / PAGE_SIZE);
showMessages();
showPagination();
// 绑定提交按钮的点击事件
const submitButton = document.getElementById('submit');
submitButton.addEventListener('click', () => {
const nameInput = document.getElementById('name');
const contentInput = document.getElementById('content');
const name = nameInput.value.trim();
const content = contentInput.value.trim();
if (name && content) {
addMessage(name, content);
nameInput.value = '';
contentInput.value = '';
maxPage = Math.ceil(messages.length / PAGE_SIZE);
currentPage = maxPage - 1;
showMessages();
showPagination();
} else {
alert('姓名和留言内容不能为空!');
}
});
// 绑定删除按钮的点击事件
const messageList = document.getElementById('message-list');
messageList.addEventListener('click', (event) => {
if (event.target.classList.contains('delete')) {
const index = parseInt(event.target.getAttribute('data-index'));
const password = prompt('请输入密码:');
if (deleteMessage(index, password)) {
maxPage = Math.ceil(messages.length / PAGE_SIZE);
if (currentPage >= maxPage) {
currentPage--;
}
showMessages();
showPagination();
} else {
alert('密码错误!');
}
}
});
}
init();
```
阅读全文