<h1>通讯录</h1> <button class="add-btn">添加联系人</button> <table> <thead> <tr> <th>姓名</th> <th>电话</th> <th>邮箱</th> <th>操作</th> </tr> </thead> <tbody> <!-- 数据渲染 --> </tbody> </table> <!-- 弹出层,用于添加或编辑联系人 --> <div id="modal" class="modal" style="display: none;"> <div class="modal-content"> <span class="close">×</span> <form> <div class="form-group"> <label for="name">姓名</label> <input type="text" id="name" name="name" required> </div> <div class="form-group"> <label for="phone">电话</label> <input type="text" id="phone" name="phone" required> </div> <div class="form-group"> <label for="email">邮箱</label> <input type="text" id="email" name="email" required> </div> <button id="save-btn">保存</button> <button id="cancel-btn">取消</button> </form> </div> </div>添加一个搜索框来进行正则查找和数据显示到五行时可以翻页
时间: 2023-06-26 21:09:59 浏览: 162
好的,以下是使用JavaScript实现的代码:
HTML部分:
```
<h1>通讯录</h1>
<button class="add-btn">添加联系人</button>
<input type="text" id="search-input" placeholder="搜索姓名或电话">
<table>
<thead>
<tr>
<th>姓名</th>
<th>电话</th>
<th>邮箱</th>
<th>操作</th>
</tr>
</thead>
<tbody id="contact-list">
<!-- 数据渲染 -->
</tbody>
</table>
<!-- 分页按钮 -->
<div id="page-buttons"></div>
<!-- 弹出层,用于添加或编辑联系人 -->
<div id="modal" class="modal" style="display: none;">
<div class="modal-content">
<span class="close">×</span>
<form>
<div class="form-group">
<label for="name">姓名</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<label for="phone">电话</label>
<input type="text" id="phone" name="phone" required>
</div>
<div class="form-group">
<label for="email">邮箱</label>
<input type="text" id="email" name="email" required>
</div>
<button id="save-btn">保存</button>
<button id="cancel-btn">取消</button>
</form>
</div>
</div>
```
CSS部分:
```
/* 省略其他样式 */
/* 分页按钮样式 */
#page-buttons {
margin-top: 20px;
display: flex;
justify-content: center;
align-items: center;
}
.page-btn {
margin: 0 10px;
padding: 5px 10px;
border: 1px solid #ccc;
cursor: pointer;
}
.page-btn.active {
background-color: #ccc;
color: #fff;
}
```
JavaScript部分:
```
// 定义全局变量
var contacts = []; // 联系人数据
var currentPage = 1; // 当前页码
var pageSize = 5; // 每页显示的数据条数
var totalPages = 1; // 总页数
// 页面加载完成后执行
window.onload = function() {
// 获取元素
var contactList = document.getElementById('contact-list');
var searchInput = document.getElementById('search-input');
var pageButtons = document.getElementById('page-buttons');
// 添加联系人按钮点击事件
var addBtn = document.querySelector('.add-btn');
addBtn.onclick = function() {
showModal();
};
// 关闭弹出层
var closeBtn = document.querySelector('.close');
closeBtn.onclick = function() {
hideModal();
};
// 取消按钮点击事件
var cancelBtn = document.getElementById('cancel-btn');
cancelBtn.onclick = function() {
hideModal();
};
// 保存按钮点击事件
var saveBtn = document.getElementById('save-btn');
saveBtn.onclick = function(event) {
event.preventDefault(); // 阻止表单提交
// 获取表单数据
var name = document.getElementById('name').value.trim();
var phone = document.getElementById('phone').value.trim();
var email = document.getElementById('email').value.trim();
// 判断表单数据是否为空
if (!name || !phone || !email) {
alert('请填写完整的联系人信息!');
return;
}
// 保存联系人数据
var contact = {name: name, phone: phone, email: email};
contacts.push(contact);
saveContacts();
hideModal();
renderContacts(); // 重新渲染联系人数据
};
// 搜索框输入事件
searchInput.oninput = function() {
renderContacts();
};
// 渲染联系人数据
function renderContacts() {
var keyword = searchInput.value.trim(); // 获取搜索关键字
var filteredContacts = contacts.filter(function(contact) {
// 过滤联系人数据
return contact.name.indexOf(keyword) !== -1 || contact.phone.indexOf(keyword) !== -1;
});
// 计算分页数据
totalPages = Math.ceil(filteredContacts.length / pageSize);
currentPage = Math.min(currentPage, totalPages);
// 渲染分页按钮
var pageBtns = '';
for (var i = 1; i <= totalPages; i++) {
pageBtns += '<div class="page-btn ' + (i === currentPage ? 'active' : '') + '">' + i + '</div>';
}
pageButtons.innerHTML = pageBtns;
// 绑定分页按钮点击事件
var pageBtnList = document.querySelectorAll('.page-btn');
for (var i = 0; i < pageBtnList.length; i++) {
pageBtnList[i].onclick = function() {
currentPage = parseInt(this.innerText);
renderContacts();
};
}
// 计算数据起始和结束索引
var startIndex = (currentPage - 1) * pageSize;
var endIndex = startIndex + pageSize;
// 渲染联系人数据
var html = '';
for (var i = startIndex; i < endIndex && i < filteredContacts.length; i++) {
var contact = filteredContacts[i];
html += '<tr>';
html += '<td>' + contact.name + '</td>';
html += '<td>' + contact.phone + '</td>';
html += '<td>' + contact.email + '</td>';
html += '<td><button class="edit-btn">编辑</button><button class="delete-btn">删除</button></td>';
html += '</tr>';
}
contactList.innerHTML = html;
// 绑定编辑按钮点击事件
var editBtnList = document.querySelectorAll('.edit-btn');
for (var i = 0; i < editBtnList.length; i++) {
editBtnList[i].onclick = function() {
var tr = this.parentNode.parentNode;
var name = tr.children[0].innerText;
var phone = tr.children[1].innerText;
var email = tr.children[2].innerText;
showModal(name, phone, email);
};
}
// 绑定删除按钮点击事件
var deleteBtnList = document.querySelectorAll('.delete-btn');
for (var i = 0; i < deleteBtnList.length; i++) {
deleteBtnList[i].onclick = function() {
var tr = this.parentNode.parentNode;
var name = tr.children[0].innerText;
var phone = tr.children[1].innerText;
var index = findContactIndex(name, phone);
if (index !== -1 && confirm('确定要删除该联系人吗?')) {
contacts.splice(index, 1);
saveContacts();
renderContacts(); // 重新渲染联系人数据
}
};
}
}
// 显示弹出层
function showModal(name, phone, email) {
var modal = document.getElementById('modal');
var title = name ? '编辑联系人' : '添加联系人';
var saveBtn = document.getElementById('save-btn');
document.querySelector('.modal-content h2').innerText = title;
document.getElementById('name').value = name || '';
document.getElementById('phone').value = phone || '';
document.getElementById('email').value = email || '';
saveBtn.innerText = name ? '保存' : '添加';
modal.style.display = 'block';
}
// 隐藏弹出层
function hideModal() {
var modal = document.getElementById('modal');
modal.style.display = 'none';
}
// 查找联系人索引
function findContactIndex(name, phone) {
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].name === name && contacts[i].phone === phone) {
return i;
}
}
return -1;
}
// 保存联系人数据到本地存储
function saveContacts() {
localStorage.setItem('contacts', JSON.stringify(contacts));
}
// 加载联系人数据
var savedContacts = localStorage.getItem('contacts');
if (savedContacts) {
contacts = JSON.parse(savedContacts);
}
renderContacts();
};
```
以上就是添加了搜索框和分页功能的通讯录代码。使用正则表达式进行匹配也可以在过滤函数中实现,但这样会影响性能,因此建议使用字符串的indexOf方法进行匹配。
阅读全文