<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" style="display: none;"> <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>帮我给这个页面用JavaScript和css3美化一下
时间: 2023-07-15 07:10:13 浏览: 157
访问通信录中的联系人和添加联系人
好的,下面是一个简单的示例代码,你可以根据自己的需要进行修改:
HTML:
```
<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>
```
CSS:
```
/* 弹出层样式 */
.modal {
display: none; /* 默认隐藏 */
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.4); /* 添加半透明背景 */
}
.modal-content {
background-color: #fefefe;
margin: 10% auto;
padding: 20px;
border: 1px solid #888;
width: 40%;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
/* 表格样式 */
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
th {
background-color: #4CAF50;
color: white;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
tr:hover {
background-color: #ddd;
}
/* 按钮样式 */
button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px;
margin: 5px;
cursor: pointer;
}
button:hover {
background-color: #3e8e41;
}
/* 表单样式 */
.form-group {
margin-bottom: 10px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type=text] {
width: 100%;
padding: 10px;
border-radius: 3px;
border: 1px solid #ccc;
box-sizing: border-box;
}
button#save-btn {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
margin-top: 10px;
cursor: pointer;
}
button#cancel-btn {
background-color: #ccc;
color: white;
border: none;
padding: 10px 20px;
margin-top: 10px;
cursor: pointer;
}
button#save-btn:hover {
background-color: #3e8e41;
}
button#cancel-btn:hover {
background-color: #bbb;
}
```
JavaScript:
```
// 获取元素
var modal = document.getElementById("modal");
var addBtn = document.querySelector(".add-btn");
var closeBtn = document.querySelector(".close");
// 点击添加联系人按钮,弹出添加联系人窗口
addBtn.addEventListener("click", function() {
modal.style.display = "block";
});
// 点击关闭按钮或者弹出层之外的区域,关闭弹出层
closeBtn.addEventListener("click", function() {
modal.style.display = "none";
});
window.addEventListener("click", function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
});
```
阅读全文