要在JavaScript中制作一个手机通讯录应用,你需要掌握一些基础的前端开发概念,包括但不限于DOM操作、事件处理、数据存储和用户界面设计。以下是一个简单的示例,展示如何使用HTML、CSS和JavaScript来创建一个基本的手机通讯录。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>手机通讯录</title>
<style>
/* 在这里添加CSS样式 */
</style>
</head>
<body>
<div id="contact-list">
<h1>通讯录</h1>
<input type="text" id="search-bar" placeholder="搜索...">
<ul id="contacts"></ul>
</div>
<script src="app.js"></script>
</body>
</html>
#contact-list {
width: 300px;
margin: auto;
}
#search-bar {
width: 100%;
padding: 10px;
margin-bottom: 10px;
}
#contacts li {
padding: 10px;
border-bottom: 1px solid #ddd;
}
// app.js
const contacts = [
{ name: '张三', phone: '12345678901' },
{ name: '李四', phone: '23456789012' },
{ name: '王五', phone: '34567890123' },
// 更多联系人...
];
const contactsList = document.getElementById('contacts');
const searchBar = document.getElementById('search-bar');
// 显示所有联系人
function displayContacts(filteredList) {
contactsList.innerHTML = '';
filteredList.forEach(contact => {
const li = document.createElement('li');
li.textContent = `${contact.name} - ${contact.phone}`;
contactsList.appendChild(li);
});
}
// 搜索联系人
searchBar.addEventListener('input', function(e) {
const searchTerm = e.target.value.toLowerCase();
const filteredContacts = contacts.filter(contact =>
contact.name.toLowerCase().includes(searchTerm) ||
contact.phone.includes(searchTerm)
);
displayContacts(filteredContacts);
});
// 初始化显示所有联系人
displayContacts(contacts);
这个示例提供了一个基本的手机通讯录应用的框架,你可以根据自己的需求进行扩展和完善。
领取专属 10元无门槛券
手把手带您无忧上云